Rumah > Artikel > pembangunan bahagian belakang > php过滤ascii控制字符_PHP教程
分享下在PHP中过滤ascii控制字符的方法。
将爬来的其它网站的数据导到xml,问题:即网页会有ascII的控制字符。
一开始以为是别人为了防止采集而加入的,然后发现一个就往过滤表里加一个。直到慢慢发现,他们都是ascii表里的字符。
解决方法:
<span>/*</span><span>* * 根据ascii码过滤控制字符 * @param type $string </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> special_filter(<span>$string</span><span>) { </span><span>if</span>(!<span>$string</span>) <span>return</span> ''<span>; </span><span>$new_string</span> = ''<span>; </span><span>for</span>(<span>$i</span> =0; <span>isset</span>(<span>$string</span>[<span>$i</span>]); <span>$i</span>++<span>) { </span><span>$asc_code</span> = <span>ord</span>(<span>$string</span>[<span>$i</span>]); <span>//</span><span>得到其asc码</span> www.jbxue.<span>com </span><span>//</span><span>以下代码旨在过滤非法字符 </span> <span>if</span>(<span>$asc_code</span> == 9 || <span>$asc_code</span> == 10 || <span>$asc_code</span> == 13<span>){ </span><span>$new_string</span> .= ' '<span>; } </span><span>else</span> <span>if</span>(<span>$asc_code</span> > 31 && <span>$asc_code</span> != 127<span>){ </span><span>$new_string</span> .= <span>$string</span>[<span>$i</span><span>]; } } </span><span>return</span> <span>trim</span>(<span>$new_string</span><span>); }</span>