php 字符串转编译utf8,php字符串如何转utf8编码

php字符串转utf8编码的方法:首先使用“mb_detect_encoding”方法自动识别字符串编码;然后通过“mb_convert_encoding”函数将其转换成国际标准编码“utf-8”即可。

ce7d49f1123607e4600150208c0d66c0.png

推荐:《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 — 检测字符的编码