preg_match_all help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdub93
Forum Newbie
Posts: 2
Joined: Thu Nov 09, 2006 11:21 am

preg_match_all help

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 []
Post Reply