在使用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); }