473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

preg_match with brackets in expression

Ok, I am using this code to check the form for invalid characters:

Expand|Select|Wrap|Line Numbers
  1. if(preg_match('/^[a-zA-Z0-9]$/', $char)){
  2. REST OF MY CODE HERE
This is what $char is:

Expand|Select|Wrap|Line Numbers
  1. $char = $_POST['charname'];
And $char connects to a form. My problem is that, I only want a-z, A-Z, 0-9, and [ and ] (aka brackets) allowed.

So far only a-z, A-Z, and 0-9 are allowed... I am not sure how to add brackets to that. Help please? Thanks!
Aug 6 '09 #1
4 7593
Dormilich
8,658 Expert Mod 8TB
escape the brackets with a backslash.
Aug 6 '09 #2
I don't want to "escape" the brackets, I want to add brackets to the exception. Currently the exception is all lowercase or capital alphabetical letters, and all 10 numbers. I want to also add ] [ to the exception [the two brackets]. How do I do this? Thanks, and I appreciate the response.
Aug 6 '09 #3
Dormilich
8,658 Expert Mod 8TB
@Untam3d
guess how you do that.....

Expand|Select|Wrap|Line Numbers
  1. preg_match('§^[a-z0-9\[\]]$§i', $char)
... by escaping
Aug 6 '09 #4
@Dormilich
Got it! Thanks a whole lot Dormilich.
Aug 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Richardson | last post by:
Apologies if this has been asked before - I can't find anything on Google or Google Groups. I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed. For some reason though, the...
2
by: splodge | last post by:
I have a simple regular expression: {1,5}x{1,5} Which is designed to detect the width and height of an image from a string, as in: foo 100x100 bar However, when I run this with...
4
by: Clodoaldo Pinto | last post by:
preg_match(): Compilation failed: regular expression too large at offset 0 The regular expression is 34,745 bytes long. <?php $regExp = 'huge regexp with 34,745 bytes'; echo...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
9
by: netimen | last post by:
I have a text containing brackets (or what is the correct term for '>'?). I'd like to match text in the uppermost level of brackets. So, I have sth like: 'aaaa 123 < 1 aaa < t bbb < a <tt ff 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.