How can I use PHP to strip out all characters that are NOT alpha, numeric, space, or puncutation?
I've tried the following, but it strip punctuation.
preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
How can I use PHP to strip out all characters that are NOT alpha, numeric, space, or puncutation?
I've tried the following, but it strip punctuation.
preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
You're going to have to list the punctuation explicitly as there is no shorthand for that (eg \s
is shorthand for white space characters).
preg_replace('/[^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?]/', '', $str);
preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $str);
Example:
php > echo preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", "⟺f✆oo☃. ba⟗r!");
foo. bar!
\p{P}
matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:
preg_replace("/[^a-zA-Z0-9\s.?!]/", "", $str);