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

How to convert string to utf8 in php

藏色散人
藏色散人 Original
2020-10-14 16:43:59 2701browse

In PHP, you can use the iconv function to convert a string into utf8 encoding. The syntax is "iconv('format to be converted', 'converted format', 'converted data');" .

How to convert string to utf8 in php

Recommended: "PHP Video Tutorial"

PHP Convert the string to the character set format UTF8/GB2312/ GBK function iconv()

iconv() introduction

iconv function can convert a known character set file into another known character set file

iconv('Format to be converted', 'Converted format', 'Converted data');

However, conversion often causes errors. Generally, you need to add "//IGNORE" after the converted encoding:

ignore means to ignore errors during conversion. Without the ignore parameter, all strings following the character cannot be saved.

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

Example:

";   echo $gb = iconv('UTF-8','GB2312',$str);   echo "
";   echo $utf = iconv('GB2312','utf-8',$gb);   echo "
";    echo $gb = iconv('GB2312','utf-8',$gb);//也可以这么用   ?>

mb_detect_encoding( $content, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));

Can determine what encoding format it is

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