php 如何检查字符串的字符集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1037363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:44:22  来源:igfitidea点击:

How to check the charset of string?

phpstringcharacter-encoding

提问by ZA.

How do I check if the charset of a string is UTF8?

如何检查字符串的字符集是否为 UTF8?

采纳答案by ZA.

function is_utf8($string) {   
return preg_match('%^(?:  
[\x09\x0A\x0D\x20-\x7E] # ASCII  
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte  
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs  
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte  
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates  
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3  
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15  
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16  
)*$%xs', $string);   

}

}

I have checked. This function is effective.

我检查过。此功能有效。

回答by soulmerge

Don't reinvent the wheel. There is a builtin function for that task: mb_check_encoding().

不要重新发明轮子。该任务有一个内置函数:mb_check_encoding().

mb_check_encoding($string, 'UTF-8');

回答by Stefan Gehrig

Just a side note:

只是一个旁注:

You cannot determine if a given string is encoded in UTF-8. You only can determine if a given string is definitively notencoded in UTF-8. Please see a related question here:

您无法确定给定的字符串是否以 UTF-8 编码。您只能确定给定的字符串是否明确以 UTF-8 编码。请在此处查看相关问题:

You cannot detect if a given string (or byte sequence) is a UTF-8 encoded text as for example each and every series of UTF-8 octets is also a valid (if nonsensical) series of Latin-1 (or some other encoding) octets. However not every series of valid Latin-1 octets are valid UTF-8 series.

您无法检测给定的字符串(或字节序列)是否是 UTF-8 编码的文本,例如每个 UTF-8 八位字节系列也是一个有效的(如果是荒谬的)Latin-1(或其他一些编码)系列八位字节。然而,并非所有有效的 Latin-1 八位字节系列都是有效的 UTF-8 系列。

回答by nikc.org

Better yet, use both of the above solutions.

更好的是,使用上述两种解决方案。

function isUtf8($string) {
    if (function_exists("mb_check_encoding") && is_callable("mb_check_encoding")) {
        return mb_check_encoding($string, 'UTF8');
    }

    return preg_match('%^(?:
          [\x09\x0A\x0D\x20-\x7E]            # ASCII
        | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
        |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
        |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
        |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
        |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
    )*$%xs', $string);

} 

回答by Jyothish V

mb_detect_encoding($string);will return the actual character set of $string. mb_check_encoding($string, 'UTF-8');will return TRUEif character set of $stringis UTF-8 else FALSE

mb_detect_encoding($string);将返回 的实际字符集$string。如果字符集为 UTF-8mb_check_encoding($string, 'UTF-8');则返回TRUE,否则返回FALSE$string

回答by David Bélanger

None of the above answers are correct. Yes, they may be working. If you take the answer with the preg_replacefunction, are you trying to kill your server if you process a lot of stirng ? Use this pure PHP function with no regex, work 100% of the time and it's way faster.

以上答案都不对。是的,他们可能正在工作。如果你用preg_replace函数来回答,如果你处理了很多搅拌,你是否试图杀死你的服务器?使用这个没有正则表达式的纯 PHP 函数,可以 100% 地工作,而且速度更快。

if(function_exists('grk_Is_UTF8') === FALSE){
    function grk_Is_UTF8($String=''){
        #   On va calculer la longeur de la cha?ne
        $Len = strlen($String);

        #   On va boucler sur chaque caractère
        for($i = 0; $i < $Len; $i++){
            #   On va aller chercher la valeur ASCII du caractère
            $Ord = ord($String[$i]);
            if($Ord > 128){
                if($Ord > 247){
                    return FALSE;
                } elseif($Ord > 239){
                    $Bytes = 4;
                } elseif($Ord > 223){
                    $Bytes = 3;
                } elseif($Ord > 191){
                    $Bytes = 2;
                } else {
                    return FALSE;
                }

                #   
                if(($i + $Bytes) > $Len){
                    return FALSE;
                }

                #   On va boucler sur chaque bytes / caractères
                while($Bytes > 1){
                    #   +1
                    $i++;

                    #   On va aller chercher la valeur ASCII du caractère / byte
                    $Ord = ord($String[$i]);
                    if($Ord < 128 OR $Ord > 191){
                        return FALSE;
                    }

                    #   Parfait
                    $Bytes--;
                }
            }
        }

        #   Vrai
        return TRUE;
    }
}

回答by Haim Evgi

if its send to u from server

如果它从服务器发送给你

echo $_SERVER['HTTP_ACCEPT_CHARSET'];