In PHP, the iconv function string is converted from GBK to UTF8 character set. In PHP, iconv function string is converted from GBK to UTF8 character set 1. iconv () description iconv function can convert one known character set file to another known character set file. For example, converting iconv function strings from GBK to UTF8 character set in G PHP
1. iconv () Introduction
The iconv function can convert a known character set file to another known character set file. Example: convert from GB2312 to UTF-8.
The iconv function is built in php Tutorial 5. the GB character set is enabled by default.
2. iconv () error
Iconv will encounter an error when converting the characters "-" to gb2312. the solution is to add "// IGNORE" after the encoding to be converted, that is, after the second parameter of the iconv function:
Iconv ("UTF-8", "GB2312 // IGNORE", $ data)
Ignore indicates that the conversion error is ignored. without the ignore parameter, all strings after this character cannot be saved.
3. iconv () example
Example 1:
Echo $ str = 'Hi, it's coffee sale! ';
Echo'
';
Echo iconv ('gb2312', 'utf-8', $ str); // Encode the string from GB2312 to UTF-8
Echo'
';
Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes
Print_r (iconv_get_encoding (); // Obtain the encoding information of the current page.
Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string.
?>
Example 2:
If your php file is UTF-8 encoded, the following code can be output correctly:
$ Str = 'I love Baidu ';
$ Utf = '';
For ($ I = 0; $ I Echo $ utf;
?> If your php file is GB, the following code can work:
$ Str = 'I love Baidu ';
$ Str = iconv ("GBK", "UTF-8", $ str );
$ Utf = '';
For ($ I = 0; $ I Echo $ utf;
?> In the above cases, the program outputs the following content:
% E6 % 88% 91% E7 % 88% B1 % E7 % 99% BE % E5 % BA % A6
Objective 1. iconv () describes how iconv functions can convert a known character set file to another known character set file. Example: from G...