PHP automatically recognizes character set encoding and completes transcoding. The principle is very simple, because gb2312gbk is a Chinese byte, the two bytes have a value range, while UTF-8 contains three Chinese characters, and each byte also has a value range. However, the principle of English is simple, because gb2312/gbk is a two-byte Chinese character, which has a value range, while UTF-8 contains three Chinese characters, each byte also has a value range. English, regardless of the encoding, is less than 128, only occupies one byte (excluding the full width)
When PHP processes the page, we use iconv or mb_convert functions for character set Conversion. However, this is actually a prerequisite. That is, we must know in and out encoding in advance before we can perform the correct conversion.
The following function can automatically determine the source string encoding and convert it without knowing the source string encoding. Although only UTF8 and GB2312 are supported, it is enough for most websites in China.
The code is as follows: |
|
Function safeEncoding ($ string, $ outEncoding = 'utf-8 ') { $ Encoding = "UTF-8 "; For ($ I = 0; $ I <128) Continue;
If (ord ($ string {$ I}) & 224) = 224) { // The first byte is determined $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { // The second byte is passed $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { $ Encoding = "UTF-8 "; Break; } } } If (ord ($ string {$ I}) & 192) = 192) { // The first byte is determined $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { // The second byte is passed $ Encoding = "GB2312 "; Break; } } } If (strtoupper ($ encoding) = strtoupper ($ outEncoding )) Return $ string; Else Return iconv ($ encoding, $ outEncoding, $ string ); } |
Example 2
The code is as follows: |
|
// Identify the Chinese character encoding. YBlog uses UTF-8. if the gb2312 encoding is referenced in the announcement, the encoding must be recognized and converted. Function safeEncoding ($ string, $ outEncoding = 'utf-8 ') { $ Encoding = "UTF-8 "; For ($ I = 0; $ I { If (ord ($ string {$ I}) <128) Continue; If (ord ($ string {$ I}) & 224) = 224) { // The first byte is determined $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { // The second byte is passed $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { $ Encoding = "UTF-8 "; Break; } } } If (ord ($ string {$ I}) & 192) = 192) { // The first byte is determined $ Char = $ string {++ $ I }; If (ord ($ char) & 128) = 128) { // The second byte is passed $ Encoding = "GB2312 "; Break; } } } If (strtoupper ($ encoding) = strtoupper ($ outEncoding )) Return $ string; Else Return iconv ($ encoding, $ outEncoding, $ string ); }
|
Bytes. English does not matter in...