UTF-8 gbk for string transcoding
$ Str = "black ";
This string I checked with the mb_detect_encoding function is UTF-8 encoded,
$ S1 = iconv ('utf-8', 'gbk', $ string );
$ S1 = mb_convert_encoding ($ string, "GBK", "UTF-8 ");
I use the above two methods for transcoding, and the results will be garbled. why?
Reply to discussion (solution)
When there are too few characters, detect will be inaccurate
Garbled characters are also affected by the output encoding. for example, if the specified page encoding is gbk, but php outputs a UTF-8 string, there are many factors.
$ Str = "black ";
If you use the mb_detect_encoding function to check for UTF-8 encoding
It indicates that your program file is UTF-8 encoded.
Since the program is UTF-8 encoded, of course, the output is UTF-8.
If you convert UTF-8 to gbk, garbled characters are inevitable.
The garbled text is normal. Your page is utf8, but you show the gbk content, so it is garbled.
You echo urlencode ($ s1); check whether the result is a two-byte result of each Chinese character % BA % DA % C9 % AB. this is the black gbk.