Home  >  Article  >  php教程  >  php自动识别编码转换为UTF-8

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

WBOY
WBOYOriginal
2016-06-06 20:08:381061browse

今天发现一段很好的代码: 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的时候,那就不能都转换编码了
上面的好处是,判断编码,然后转换,和谐了
Statement:
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