Page 1 of 1

preg_match_all help

Posted: Tue Dec 05, 2006 9:58 pm
by cdub93
For the following preg_match_all statement, I would like to also pass through slashes / and \ as well as brackets [ and ] but can't figure out how to include them
Any ideas?

Code: Select all

preg_match_all("/[a-zA-Z0-9\Ä\ä\Ö\ö\Ü\ü\_\-\.\,\=\(\)]+/"

Thanks,
Chris[/list]

Posted: Tue Dec 05, 2006 10:44 pm
by feyd
They are escaped just like the other metacharacters you have in your pattern already. The accented letters shouldn't need escaping, nor should underscore, comma, and equals.

Posted: Tue Dec 05, 2006 11:03 pm
by volka
You chose / as delimiter, therefore you have to escape it within the expression. I suggest choosing another delimiter, e.g. !
pcre uses \ as escape-sign, a \-character is represented by \\. But php uses the same mechanism

Code: Select all

echo '\\';
prints just \
http://de2.php.net/reference.pcre.pattern.syntax wrote:Note: Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\" or '\\\\' must be used in PHP code.
If your locale is set correctly [:alnum:] should match the whole a-zA-Z0-9äöüÄÖÜß part.

Code: Select all

$pattern = '![[:alnum:]\\\\/.,;+]+!';
$subject = 'ßabcäöÜß098';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
But you might not want to rely on the locales setting.

edit: re-posted as

Code: Select all

instead of

Code: Select all

, otherwise [:alnum:] becomes []