RE: [PHP] preg_match problem

From: Date: Tue, 23 Jan 2007 01:14:08 +0000
Subject: RE: [PHP] preg_match problem
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-247588@lists.php.net to get a copy of this message
At 1/22/2007 04:56 PM, Beauford wrote:
I've probably read 100 pages on this, and no matter what I try it doesn't work. Including all of what you suggested above - is my PHP possessed? if(preg_match("/^[A-Za-z0-9!@#$%&()*;:_.'/\\ ]+$/", $string)) { gives me this error. Warning: preg_match() [function.preg-match]: Unknown modifier '\' in /constants.php on line 107 So if If you wouldn't mind, could you show me exactly what I need right from the beginning and explain why it works. i.e. if(preg_match(what goes here", $string)) { Echo "You got it";
Beauford, Because you're using forward slashes /.../ to delimit your pattern, regexp is using the / in your character class to terminate your pattern:
        /^[A-Za-z0-9!@#$%&()*;:_.'/
Then it's looking at the subsequent characters as pattern modifiers and gacking on the backslash. Even if you had a character there that it could accept as a pattern modifier, the character class would be incomplete (no closing bracket) so the pattern is doomed to fail. You need to escape that forward slash in the character class:
        preg_match("/^[A-Za-z0-9!@#$%&()*;:_.'\/
Also, you've got only two backslashes in your char class. PHP is reducing this to a single backslash before the space character. I think you intend this to be two backslashes in the pattern so you need four backslashes in PHP:
        preg_match("/^[A-Za-z0-9!@#$%&()*;:_.'\/\\\\ ]+$/", $string)
Regards, Paul __________________________ Juniper Webcraft Ltd. http://juniperwebcraft.com

« previous php.general (#247588) next »