Home  >  Article  >  Backend Development  >  How to filter invisible characters in php

How to filter invisible characters in php

藏色散人
藏色散人Original
2020-07-30 09:56:433462browse

php method to filter invisible characters: first create a PHP code sample file; then define a "filterNonPrintableChar" method; then implement filtering logic through isset, ord and other functions; finally output the filtering results through "var_dump" Can.

How to filter invisible characters in php

Recommended: "PHP Video Tutorial"

php filter invisible characters

Coding knowledge

ASCII codes are reserved for standard symbols, numbers, English, etc. The value range is 0~127, and some are 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 = &#39;&#39;;
    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));

The above is the detailed content of How to filter invisible characters in php. For more information, please follow other related articles on the PHP Chinese website!

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