Home > Article > Backend Development > How to filter ascii control characters in php_PHP tutorial
I still remember that at work, I imported data from other websites to xml. But you will encounter a problem: the web page will have asciII control characters. At first, I thought it was added by someone else to prevent collection. Then I found one and added it to the filtering list. Until I slowly discovered that they are all characters in the ascii table. Once you find the cause, it will be easy to solve.
/**
* Filter control characters according to ascii code
* @param type $string
*/
public static function special_filter($string)
{
if(!$string) return '';
$new_string = '';
for($i =0; isset($string[$i]); $i++)
{
$asc_code = ord($string[$i]); //Get its asc code
//The following code is designed to filter illegal characters
if($asc_code == 9 $asc_code == 10 $asc_code == 13){
$new_string .= ' ';
}
else if($asc_code > 31 && $asc_code != 127){
$new_string .= $string[$i];
}
}
return trim($new_string);
}