php小技巧之過濾ascii控制字符
更新時間:2014年05月14日 10:29:41 作者:
分享一個小技巧,在PHP中如何過濾ascii控制字符,需要的朋友可以參考下
還記得以前在工作中,將爬來的其它網站的數據導到xml。但是會遇到一個問題:即網頁會有ascII的控制字符。一開始以為是別人為了防止采集而加入的,然后發(fā)現一個就往過濾表里加一個。直到慢慢發(fā)現,他們都是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碼
//以下代碼旨在過濾非法字符
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);
}
相關文章
PHP中的排序函數sort、asort、rsort、krsort、ksort區(qū)別分析
在php中自帶了大量了數組排序函數,下面我們一一來介紹一下關于php數組排序的用法吧。2014-08-08Fatal error: Allowed memory size of 134217728 bytes exhauste
這篇文章主要介紹了Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)錯誤的解決方法,需要的朋友可以參考下2014-11-11