php字符串转utf8编码的方法:首先使用“mb_detect_encoding”方法自动识别字符串编码;然后通过“mb_convert_encoding”函数将其转换成国际标准编码“utf-8”即可。
推荐:《PHP视频教程》
在使用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);
}
mb_convert_encoding — 转换字符的编码
mb_detect_encoding — 检测字符的编码