【php识别中文版】php识别中文编码并自动转换为UTF-8

时间:2019-07-03  来源:php函数  阅读:

php自动识别编码,若里面有中文的话,将其转换为UTF-8就最好了,因为中文在Gbk编辑情况情况下,有可能会乱码,这个和客户端和服务端编码都有关系,为了避免乱码,我们可以使用下面的函数将其自动转换为UTF8国际标准编码:

 代码如下

function characet($data){
  if( !empty($data) ){
    $fileType = mb_detect_encoding()($data , array("UTF-8","GBK","LATIN1","BIG5")) ;
    if( $fileType != "UTF-8"){
      $data = mb_convert_encoding($data ,"utf-8" , $fileType);
    }
  }
  return $data;
}
?>


补充:文件编码转换

 代码如下

$temstr=file_get_contents($path);
$encode   = mb_detect_encoding($temstr,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW");   
$temstr=mb_convert_encoding($temstr,   "CP936",   $encode); 

例子

 代码如下

$f=fopen("test.txt", "wb");  
$text=utf8_encode("a!");  
 
//先用函数utf8_encode将所需写入的数据变成UTF编码格式。  
 
$text="\\xEF\\xBB\\xBF".$text;  
 
//"\\xEF\\xBB\\xBF",这串字符不可缺少,生成的文件将成为UTF-8格式,否则依然是ANSI格式。  
 
fputs($f, $text);  
 
//写入。  
 
fclose($f);  
?>  

【php识别中文版】php识别中文编码并自动转换为UTF-8

http://m.bbyears.com/jiaocheng/56283.html

推荐访问:php中文官网 php是什么意思中文 php中文乱码
相关阅读 猜你喜欢
本类排行 本类最新