php gbk to utf8 method: first create a PHP sample file; then set the header; finally use the "iconv('GB2312', 'UTF-8', $str);" method to convert the string Just change the encoding from GB2312 to utf8.
The operating environment of this tutorial: windows10 system, php5.6. This article is applicable to all brands of computers.
Recommended: "PHP Video Tutorial"
Convert gbk to utf8
<?php header("Content-type:text/html;charset=GB2312"); echo $str= '你好,这里是gbk转utf8!'; echo '<br />'; echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8
iconv — Strings are converted according to the required character encoding
Description
iconv ( string $in_charset , string $out_charset , string $str ) : string
Convert the string str from in_charset to out_charset.
Parameters
in_charset
Input character set.
out_charset
Output character set.
If you add the string //TRANSLIT after out_charset, the transliteration function will be enabled. This means that when a character cannot be represented by the target character set, it can be approximated by one or more similar characters. If you add the string //IGNORE, characters that cannot be expressed in the target character set will be silently discarded. Otherwise, an E_NOTICE is caused and FALSE is returned.
Caution
//TRANSLIT operation details are highly dependent on the system's iconv() implementation (see ICONV_IMPL). It is reported that the implementation on some systems will directly ignore //TRANSLIT, so the conversion may fail and out_charset will be unqualified.
str
The string to be converted.
Return Value ¶
Returns the converted string, or FALSE on failure.
The above is the detailed content of How to convert php gbk to utf8. For more information, please follow other related articles on the PHP Chinese website!