Reputation: 4701
I'm trying to figure out what this does, and how to change it.
preg_replace('/[^\w\s]/','',$var);
I THINK this replaces all symbols with nothing (deletes them basically). What if I want to allow some specific symbols, what do I change here?
For example I want replace everything EXCEPT:
Thanks.
Upvotes: 0
Views: 64
Reputation: 13257
^
means everything but this, so this replaces everything but word characters (\w
) and whitespace (\s
).
To replace everything except the characters you mention, use this:
preg_replace('/[^\w\s\/]/', '', $var);
\/
is the escaped version of /
.
Upvotes: 2
Reputation: 2966
that is basically: "find everything that's not a word character or a space character and remove it from the string"
^ is a negation, so you can put whatever you want after that in the [] and it will skip those. It is already doing everything you want except for the /
Note : \w is shorthand for [0-9A-Za-z_] (or close to it.)
from a perl.org
\w is a word character (alphanumeric or ) and represents [0-9a-zA-Z]
Upvotes: 1