Share how to filter ascii control characters in PHP.
Export the crawled data from other websites to xml. 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 filter list. Until I slowly discovered that they are all characters in the ascii table.
Solution:
<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>