Home  >  Article  >  Backend Development  >  How to filter ascii control characters in php_PHP tutorial

How to filter ascii control characters in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:02851browse

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);

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/814671.htmlTechArticleI 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 someone else trying to prevent picking...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn