Home  >  Article  >  Backend Development  >  Method for PHP to automatically identify file encoding and convert to UTF-8_PHP Tutorial

Method for PHP to automatically identify file encoding and convert to UTF-8_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:28912browse

Converting file encoding in PHP is a relatively simple matter, but when passing Chinese parameters during development, sometimes you don’t know what encoding it is, resulting in garbled characters. Here is a very convenient solution that automatically recognizes the encoding and converts it to UTF-8. The specific code is as follows:

Copy code The code is as follows:
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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/788614.htmlTechArticlePHP converting file encoding is a relatively simple matter, but when passing Chinese parameters during development, sometimes it is not possible to I know what encoding it is, but the result is garbled characters. Here is a...
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