Home >Backend Development >PHP Tutorial >Automatically identify text encoding and convert to target encoding!
When PHP processes pages, we use functions such as iconv or mb_convert to convert character sets, but this actually has a premise. That is, we must know in advance what encoding in and out are so that we can perform the correct conversion. Although most conversions are between gbk and utf-8, what if you don’t know the encoding of the conversion object? Google has come up with such a function safeEncoding, which can easily identify the encoding of UTF8 and GBK. This function is very accurate to a certain extent, but it is not so easy to use in some more complex environments. Below I combine the differences between GBK and UTF-8 encoding and use regular expressions to determine UTF-8 Encode and use the mb_convert_encoding function to convert. In China, the most popular encodings are GBK and UTF-8, so this function automatically converts these two encodings.
|