php字符串自动转utf-8编码

来自:互联网
时间:2020-03-19
阅读:

在使用mb_convert_encoding时要先知道字符编码,如果编码错误就会乱码,使用mb_detect_encoding自动识别字符串编码,并转换成国际标准编码utf-8编码。

<?php
$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5','LATIN1'));
if($encode != 'UTF-8'){
    $name = mb_convert_encoding($name, 'UTF-8', $encode);
}
返回顶部
顶部