Home  >  Article  >  Backend Development  >  PHP filter ascii control characters_PHP tutorial

PHP filter ascii control characters_PHP tutorial

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

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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769757.htmlTechArticle Share how to filter ascii control characters in PHP. Import the data crawled from other websites into xml. The problem is: the web page will have ascii control characters. At first I thought it was someone else doing it to prevent...
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