Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:08:38
Original
1010 people have browsed it

今天发现一段很好的代码: 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; 这个

今天发现一段很好的代码:

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



这个在于传中文参数的时候,由于编码不同,会发生乱码现象,当参数编码不一定是GBK的时候,那就不能都转换编码了
上面的好处是,判断编码,然后转换,和谐了
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!