Home > php教程 > php手册 > php自动识别文件编码并转换为UTF-8的方法

php自动识别文件编码并转换为UTF-8的方法

WBOY
Release: 2016-06-13 09:33:57
Original
1100 people have browsed it

PHP转换文件编码是一个比较简单的事情,但是在开发中传递中文参数的时候,有时候不知道是什么编码,结果造成了乱码的现象。这里有个非常方便的解决办法,可以自动识别编码并转换为UTF-8。具体代码如下:

复制代码 代码如下:

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

Related labels:
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