Home > Backend Development > PHP Tutorial > php字符串怎么转utf-8编码

php字符串怎么转utf-8编码

PHPz
Release: 2020-09-05 09:46:11
Original
4071 people have browsed it

php字符串转utf-8编码的方法:首先通过“mb_detect_encoding”函数检测字符的编码;然后使用“mb_convert_encoding”函数转换字符的编码即可。

php字符串怎么转utf-8编码

php字符串怎么转utf-8编码?

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

<?php
$encode = mb_detect_encoding($str, array("ASCII",&#39;UTF-8&#39;,"GB2312","GBK",&#39;BIG5&#39;,&#39;LATIN1&#39;));
if($encode != &#39;UTF-8&#39;){
    $name = mb_convert_encoding($name, &#39;UTF-8&#39;, $encode);
}
Copy after login

更多相关知识,请访问PHP中文网

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template