还记得以前在工作中,将爬来的其它网站的数据导到xml。但是会遇到一个问题:即网页会有ascII的控制字符。 一开始以为是别人为了防止采集而加入的,然后发现一个就往过滤表里加一个。直到慢慢发现,他们都是ascii表里的字符。 找到原因了,就好解决了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 根据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码
//以下代码旨在过滤非法字符
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);
}