Here is the php function to remove all character which are not alphabets or numbers to remove the white space just pass the param $removeSpace as 1 function removeNonAlphaNumero( $str , $removeSpace = 0 ){ $return_string = ""; if( trim($str) != "" ){ if($removeSpace ) $return_string = preg_replace("/[^a-zA-Z0-9]/", "", $str); else $return_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $str); } return $return_string; }