Skip to content
Advertisement

Strip all non-alphanumeric, spaces and punctuation symbols from a string

How can I use PHP to strip out all characters that are NOT letters, numbers, spaces, or punctuation marks?

I’ve tried the following, but it strips punctuation.

preg_replace("/[^a-zA-Z0-9s]/", "", $str);

Advertisement

Answer

preg_replace("/[^a-zA-Z0-9sp{P}]/", "", $str);

Example:

php > echo preg_replace("/[^a-zA-Z0-9sp{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-9s.?!]/", "", $str);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement