Home >Backend Development >PHP Problem >How to convert php string to utf8 encoding

How to convert php string to utf8 encoding

藏色散人
藏色散人Original
2020-09-07 09:52:273077browse

How to convert php string to utf8 encoding: first use the "mb_detect_encoding" method to automatically identify the string encoding; then use the "mb_convert_encoding" function to convert it into the international standard encoding "utf-8".

How to convert php string to utf8 encoding

Recommended: "PHP Video Tutorial"

When using mb_convert_encoding, you must first know the character encoding. If the encoding is wrong It will be garbled. Use mb_detect_encoding to automatically identify the string encoding and convert it to the international standard encoding utf-8 encoding.

<?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);
}

mb_convert_encoding — Convert character encoding

mb_detect_encoding — Detect character encoding

The above is the detailed content of How to convert php string to utf8 encoding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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