English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Judgment String Encoding is utf-8 Or gb2312Example

PHP Judgment String Encoding is utf-8 Or gb2312

First method:

function is_gb2312($str)
{
    for($i=0; $i<strlen($str); $i++) {
        $v = ord( $str[$i] );
        if( $v > 127) {
            if( ($v >= 228) && ($v <= 233) )
            {
                if( ($i+2>= (strlen($str)) - 1)) return true; // not enough characters
                $v1 = ord( $str[$i+1] );
                $v2 = ord( $str[$i+2] );
                if( ($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191) ) // utf encoding
                    return false;
                else
                    return true;
            }
        }
    }
    return true;
}

Second method:

/**
 * determine if the string is utf-8 or gb2312
 * @param unknown $str
 * @param string $default
 * @return string
 */
public static function utf8_gb2312($str, $default = 'gb2312)
{
 $str = preg_replace("/[\x01-\x7F]+/", "", $str);
 if (empty($str)) return $default;
 $preg = array(
 "gb2312" => "/^([\xA1-\xF7][\xA0-\xFE])+$/", //Regular expression judgment whether it is gb2312
 "utf-8" => "/^[\x{4E00}-\x{9FA5}]+$/u",   //Regular expression judgment whether it is Chinese character (utf8Encoding conditions have been included), this range actually includes traditional Chinese characters
 );
 if ($default == 'gb2312) {
 }$option = 'utf-8';
 }
 $option = 'gb2312';
 }
 if (!preg_match($preg[$default], $str)) {
 return $option;
 }
 $str = @iconv($default, $option, $str);
 //Cannot be converted to $option, indicating that the original is not $default
 if (empty($str)) {
 return $option;
 }
 return $default;
}

Thank you for reading, I hope it can help you. Thank you for your support to this site!

You May Also Like