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

WBOY
Release: 2016-07-13 10:28:28
Original
913 people have browsed it

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...
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 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!