RE: [PHP] Simple REGEX?

From: Date: Sun, 18 Feb 2001 15:00:45 +0000
Subject: RE: [PHP] Simple REGEX?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40419@lists.php.net to get a copy of this message
well, if the only characters that you want to allow are alphanum/space/underscore, you could use a perl regexp to match against anything *other* than those things...(a positive match would indicate that the string being matched had invalid characters in it) use this: preg_match('/[^\w\s]/', $input); (to see an explanation of why this works, please read the bottom of my email... i give a short breakdown of it) if you want to allow any more characters, simply place the character you want to allow within the brackets, and any instance of that character in the string to be matched will be ignored (rather than matched) for example, to allow periods and question marks in your input (without getting a positive match on the regexp), modify it to look like this: '/[^\w\s\.\?]/' what will match and what will not match using this regexp: print preg_match('/[^\w\s]/', $t); $t = "ea!ti_t99"; will print "1" since the "!" will match the regexp $t = "Hello one_two" will print "0" since nothing matches $t = "How *are* you" will print "1" since the "*" matches the regexp works like this: [ ] = character class ^ = not \w = alphanumeric \s = space the character class brackets are just to let the regexp know that the things inside of it are to be matched one by one against the string. "[suv]" would not match the entire string "suv", but rather "s" or "u" or "v", so "[\w\s]" would match any string that had an alphanumeric character or whitespace in it. since you want to find strings that have NOT-alphanum or NOT-space, you simply negate the character class that will match alphanum and space, by adding a "^" to it... making it "[^\w\s]" (match anything that is NOT an alphanumeric or space character - meaning match any string that has invalid characters). if you want to allow other characters into your input, just add them into the character class. if you find that you need to allow "?" into your input, use this "[^\w\s\?]"... and it will match any string that does NOT have alphanum/space/?, meaning that it will match any string with invalid chars..... > -----Original Message----- > From: James, Yz [mailto:liljim@btconnect.com] > Sent: Saturday, February 17, 2001 12:15 > To: php-general@lists.php.net > Subject: [PHP] Simple REGEX? > > > Hi guys, > > I'd like to restrict the characters a client is inputting to a database. > > I already have a system that'll exclude certain characters. Alpha Numerics > are permitted, together with the underscore and spaces. Is there a > collective expression for the following characters? : > > !",.?@() > > Because I'd like to allow those too. And I'm useless with RegEx. > > Thanks as always, > James. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#40419) next »