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.

JavaScript

Advertisement

Answer

JavaScript

Example:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement