Home  >  Article  >  Backend Development  >  php过滤ascii控制字符_PHP教程

php过滤ascii控制字符_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:29:451196browse

分享下在PHP中过滤ascii控制字符的方法。

将爬来的其它网站的数据导到xml,问题:即网页会有ascII的控制字符。

一开始以为是别人为了防止采集而加入的,然后发现一个就往过滤表里加一个。直到慢慢发现,他们都是ascii表里的字符。

解决方法:

/** 
* 根据ascii码过滤控制字符 
* @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]); //得到其asc码
www.jbxue.com
//以下代码旨在过滤非法字符 
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/769757.htmlTechArticle分享下在PHP中过滤ascii控制字符的方法。 将爬来的其它网站的数据导到xml,问题:即网页会有ascII的控制字符。 一开始以为是别人为了防止...
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