Re: preg_match problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 1/22/2007 03:04 PM, Beauford wrote:
I'm trying to get this but not quite there. I want to allow the following
characters.

A-Za-z0-9!@#$%&()*;:_.'/\ and a space.

Is there a special order these need to be in or escaped somehow. For
example, if I just allow _' the ' is fine, if I add the other characters,
the ' gets preceded by a \ and an error is returned. If I take the ' out of
the sting below it works fine, I get no errors returned. I am also using
stripslashes on the variable.

This is my code. (although I have tried various things with it trying to get
it right)

if(preg_match("/^[-A-Za-z0-9!@#%&\(\)\*;:_.\'\$ ]+$/", $string)) {


Please read this page:

Pattern Syntax -- Describes PCRE regex syntax
http://ca.php.net/manual/en/reference.pcre.pattern.syntax.php

specifically the section on character classes:
http://ca.php.net/manual/en/reference.pcre.pattern.syntax.php#regexp.reference.squarebrackets

In PREG there are few characters that you need to escape in a character class expression:

1) ^ must be escaped (\^) if it's the first character (it negates the match when it's unescaped in the first position).

2) ] must be escaped (\]) unless it's the first character of the class (it closes the class if it appears later and is not escaped).

3) \ must be escaped (\\) if you're referring to the backslash character rather than using backslash to escape another character.

4) Control codes such as \b for backspace (not to be confused with \b which means word boundary outside of a character class).


In addition, you may need to escape certain characters in PHP if you're expressing the RegExp pattern in a quoted string, such as single or double quotes (whichever you're using to quote the pattern):

        '[\'"]'  escape the apostophe
        "['\"]"  escape the quotation mark

Those are PHP escapes -- by the time PREG sees the pattern, the PHP compiler has rendered it to:

        ['"]

And then of course there's the complication that both PREG and PHP use the backslash as the escape character, so the pattern:

        [\\]  escape the backslash

must be expressed in PHP as:

        [\\\\]          escape the escape and the backslash

Interestingly, PHP compiles both \\\ and \\\\ as \\, go figure. I use \\\\ to escape both backslashes to maintain some semblance of logic and order in these eye-crossing expressions.

Because PHP requires quotes to be escaped, I find it easier to write & debug patterns in PHP if I express them in heredoc where quoting and most escaping is unnecessary:

$sPattern = <<<_
['"\\\\]
_;

becomes the PREG pattern ['"\\].


So to address your character class:

A-Za-z0-9!@#$%&()*;:_.'/\ and a space.

I'd use the pattern:

        [A-Za-z0-9!@#$%&()*;:_.'/\\ ]

where the only character I need to escape is the backslash itself.

In PHP this would be:

        $sPattern = '[A-Za-z0-9!@#$%&()*;:_.\'/\\\\ ]';
        (escaped apostrophe & blackslash)
or:
        $sPattern = "[A-Za-z0-9!@#$%&()*;:_.'/\\\\ ]";
        (escaped blackslash)
or:
        $sPattern = <<<_
[A-Za-z0-9!@#$%&()*;:_.'/\\\\ ]
_;
        (escaped blackslash)

Regards,

Paul
__________________________

Juniper Webcraft Ltd.
http://juniperwebcraft.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux