How to check the charset of string?

前端 未结 7 1704
心在旅途
心在旅途 2021-02-04 08:48

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

7条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 09:43

    None of the above answers are correct. Yes, they may be working. If you take the answer with the preg_replace function, 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.

    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;
        }
    }
    

提交回复
热议问题