Remove Non-Alphanumeric Characters from a String in PHP

Remove all non-alphanumeric characters from a string, which aren’t in a-zA-Z0-9 range or which are not spaces:

preg_replace('/[^A-Za-z0-9 ]/i', '', $string);

If you want to support unicode characters:

preg_replace('/[^[:alnum:][:space:]]/u', '', $string);

More Related Posts