How to convert php string to utf8 encoding

藏色散人
Release: 2023-03-05 19:14:02
Original
3059 people have browsed it

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);
}
Copy after login

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!

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