Strip non-alphanumeric characters using PHP

Posted by on 8 October 2008

You can use regular expressions to achieve this very simply.

function clean_query_string($vsString) {
     return ereg_replace("[^A-Za-z0-9-]", "", $vsString);

The regular expression, [^A-Za-z0-9-] tells the function to replace any character that is not (^ means not) any of these characters, A-Za-z0-9-

This would be useful when you need to clean a query string value or the like.

Like to make an inquiry? Contact Us