Search code examples
phpregexstringpreg-replacesanitization

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-9\s]/", "", $str);

Solution

  • 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);