php method for processing invisible characters: first create a PHP file; then define a filterNonPrintableChar method; then implement the functional logic of filtering invisible characters through the while statement in the method body; and finally run the file.
php filters invisible characters
Encoding knowledge
ASCII code is standard Symbols, numbers, English, etc. are reserved, and the value range is 0~127, and some are used as extended ASCII codes 128~255. When the operating system uses non-ASCII encoding (such as Chinese character encoding), extended ASCII code is generally used. It is agreed to use a coding range of 128 to 255 for 2 to 3 or even 4 consecutive bytes to encode Chinese characters.
So you can judge whether the ASCII code decimal value is greater than 127 to filter out Chinese characters.
php code
<?php /** * @param $str * @return string * 过滤不可见字符,支持中文过滤 * 0至31和127这33个编码是不可见的特殊字符(控制符) */ function filterNonPrintableChar($str) { $i = 0; $newStr = ''; while (isset($str[$i])) { $char = $str[$i]; $asc = ord($char); if ($asc > 31 && $asc < 127 || $asc > 127) { $newStr .= $char; } $i++; } return $newStr; } $str = chr(1) ."a3#2%1". chr(2) . chr(54) . "s哈哈ad~a好的s"; var_dump(filterNonPrintableChar($str)); $str = "“ASCII码为标准符号、数字、英文等进行了保留,取值范围是0~127,还有一部分作为扩展ASCII码128~255当操作系统采用非ASCII编码时(比如汉字编码),一般用扩展ASCII码来进行,约定用128~255范围的编码连续2~3甚至4个来进行...”"; var_dump(filterNonPrintableChar($str));
Recommended: "PHP Tutorial"
The above is the detailed content of How to deal with invisible characters in php. For more information, please follow other related articles on the PHP Chinese website!