php中文乱码

来源:互联网 发布:cinema 4d r18 mac 编辑:程序博客网 时间:2024/06/01 22:06

http://www.jb51.net/article/27282.htm

 

public function togbk($str){
    $encode = mb_detect_encoding($str,array("ASCII","GB2312","UTF-8","GBK","BIG5"));
    if ($encode=="UTF-8"){
        $str = iconv("utf-8","GBK//IGNORE",$str);
    }
    return ($str);
}

 

http://zhidao.baidu.com/link?url=-R8u6MoBvve0p2RHmVmR3mZeAMRmBY4aV5osqg2LT1kS7GrjyvrPbuOBE5129HoOipRUGkPbWm_bGTFHZHozuq


判断是否为utf8

if($this->is_utf8($userid)==1){
    $userid = iconv("utf-8","GBK",$userid);
}

 

functionis_utf8($word)
{
    if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)
    {
        return true;
    }
    else
   
{
        return false;
    }

}


判断中文:

$str = "中文"; 
if (preg_match("/[\x7f-\xff]/", $str)) { 
echo "含有中文"; 
}else{ 
echo "没有中文"; 

或 
$pattern = '/[^\x00-\x80]/'; 
if(preg_match($pattern,$str)){ 
echo "含有中文"; 
}else{ 
echo "没有中文"; 
}

0 0
原创粉丝点击