Home >Backend Development >PHP Problem >How to convert string to utf8 in php
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');" .
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:
<?php header("content-type:text/html;charset=utf8"); echo $str = "你好,你是卖咖啡的嘛?"; echo "<br>"; echo $gb = iconv('UTF-8','GB2312',$str); echo "<br>"; echo $utf = iconv('GB2312','utf-8',$gb); echo "<br>"; 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!