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

原文地址:http://www.codefans.net/articles/1272.shtmlphp

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

01 <?php
02 function characet($data){
03   if( !empty($data) ){
04     $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;
05     if( $fileType != 'UTF-8'){
06       $data = mb_convert_encoding($data ,'utf-8' , $fileType);
07     }
08   }
09   return $data;
10 }
11 ?>
相关文章
相关标签/搜索