Replace All Characters In a String that Aren’t Alphanumeric

Using the preg_replace function you can easily replace all characters in a string that are not alphanumeric. This can be useful for sanitizing a string or user input.

$newString = preg_replace("/[^A-Za-z0-9 ]/", "", $text);

Leave a Reply

Your email address will not be published.