A quite common request received by PHPRO is to strip all “bad characters” except for a space. Bad characters are defined in these requests as any non alpha numeric characters. Most often these requests are from people who wish to sanitize postal addresses or the like. This simple function provides a basic wrapper for the PHP function preg_match to strip out the unwanted characters and returns a sanitized string, including white space characters.
function alphanumericAndSpace( $string )
return preg_replace('/[^a-zA-Z0-9\s]/', '', $string);