php中常用的字符转换函数iconv 和 mb_convert_encoding
1、iconv使用方法:
iconv("原编码", "目标编码//TRANSLIT", 需要处理的字符串);
utf8转换为gbk
<?php
header("Content-type:text/html;charset=UTF-8");
echo $str= '你好,这里是utf8转gbk!';
echo '<br />';
echo iconv("UTF-8","gbk//TRANSLIT",$str); //将字符串的编码从UTF-8转到GB2312
gbk转换为utf8
<?php
header("Content-type:text/html;charset=GB2312");
echo $str= '你好,这里是gbk转utf8!';
echo '<br />';
//将字符串的编码从GB2312转到UTF-8
echo iconv('GB2312', 'UTF-8', $str);
2、mb_convert_encoding
mb_convert_encoding(需要处理的字符串, 目标编码, 原编码);
3、识别中文编码并自动转换为UTF-8
function charset($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; }
来源:https://www.cnblogs.com/WebLinuxStudy/p/12421632.html