preg_match_all ( '/[^a-z0-9_ -&]/i' , $string , $matches );
Help matching all symbols except defined
Started by Torrodon, Jun 07 2009 06:10 AM
3 replies to this topic
#1
Posted 07 June 2009 - 06:10 AM
I need to match all symbols except letters, numbers, "_", " ", "-", "&"? I tried the following but it is not working:
|
|
|
#2
Guest_Jordan_*
Posted 07 June 2009 - 06:18 AM
Guest_Jordan_*
You need to escape your space, -, and &. You also probably want to include uppercase letters also?
Note, this is untested.
preg_match_all ( '/[^A-Za-z0-9_\ \-\&]/i' , $string , $matches );
Note, this is untested.
#3
Posted 07 June 2009 - 07:07 AM
Jordan, you don't need to escape the space or ampersand. You only need to escape characters with special meaning like the hyphen inside a character class (or you can place the hyphen at the end). Also the `i' modifier at the end makes the expression case insensitive so the A-Z is redundant. I would use
The meta character \w matches any alphanumeric character plus the underscore
The meta character \s matches a space
Note: This expression only matches one character. If you want it to match more than one you need to add a star or plus quantifier to the character class.
'/[^\w\s&-]/i'It should do the same thing, but its a lot easier to read.
The meta character \w matches any alphanumeric character plus the underscore
The meta character \s matches a space
Note: This expression only matches one character. If you want it to match more than one you need to add a star or plus quantifier to the character class.
#4
Posted 08 June 2009 - 06:57 PM
grrr i forgot '-' is special symbol inside [] and i have to escape it
thanks
thanks


Sign In
Create Account

Back to top









