代码之家  ›  专栏  ›  技术社区  ›  ZA.

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

  •  12
  • ZA.  · 技术社区  · 17 年前

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

    7 回复  |  直到 9 年前
        1
  •  33
  •   soulmerge    17 年前

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

    mb_check_encoding($string, 'UTF-8');
    
        2
  •  15
  •   Community Mohan Dere    9 年前

    只是一个旁注:

    无法确定给定字符串是否以UTF-8编码。只能确定给定字符串是否为 以UTF-8编码。请看相关问题 here :

    无法检测给定的字符串 (或字节序列)是UTF-8编码的 例如,每个 一系列的utf-8八位字节也是有效的 (如果是无意义的)拉丁语-1系列(或 其他一些编码)八位字节。然而 并非所有拉丁语系列都有效-1 八位字节是有效的UTF-8系列。

        3
  •  6
  •   ZA.    17 年前
    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);   
    

    }

    我查过了。此功能有效。

        4
  •  6
  •   nikc.org    17 年前

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

    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);
    
    } 
    
        5
  •  1
  •   Jyothish V    9 年前

    mb_detect_encoding($string); 将返回的实际字符集 $string . mb_check_encoding($string, 'UTF-8'); 将返回 真的 if字符集 $字符串 是UTF-8 错误的

        6
  •  0
  •   Haim Evgi    17 年前

    如果它从服务器发送到U

    echo $_SERVER['HTTP_ACCEPT_CHARSET'];
    
        7
  •  0
  •   David Bélanger    13 年前

    以上答案都不正确。是的,他们可能在工作。如果你用 preg_replace 函数,如果你处理了大量的string,你是不是想杀死你的服务器?使用这个没有regex的纯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;
        }
    }
    
    推荐文章