Home>Article>Backend Development> How to convert string to utf8 format in php

How to convert string to utf8 format in php

醉折花枝作酒筹
醉折花枝作酒筹 Original
2021-04-02 10:59:31 2978browse

Method: 1. Use iconv function, syntax "iconv(original character type, 'UTF-8', string)"; 2. Use mb_convert_encoding function, syntax "mb_convert_encoding(string, 'UTF-8" ', original character type)".

How to convert string to utf8 format in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

1. Use the iconv() function
iconvfunction can convert a known character set file into another known character set file. For example: Convert fromGB2312toUTF-8. iconv will make an error when converting the characters "—" to "gb2312". The solution is to add "//IGNORE" after the encoding that needs to be converted, which is the second iconv function. parameters. As follows:

iconv("UTF-8", "GB2312//IGNORE", $data)

ignoremeans to ignore errors during conversion. Without theignoreparameter, all strings following this character cannot be saved.

';   echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8 ?>

2. Use "mb_convert_encoding" to convert the string into the UTF8 character set

When usingmb_convert_encoding, you must first know the character encoding. If If the encoding is wrong, the characters will be garbled. Usemb_detect_encodingto automatically identify the string encoding and convert it to the international standard encoding utf-8 encoding.

Recommended tutorial:php video tutorial

The above is the detailed content of How to convert string to utf8 format in php. 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