Chr () in PHP is the conversion between ASCII characters and corresponding numbers.
But why the following code can output Chinese characters?
For example, Chinese characters ' sense ' unt-8 coding is e6849f;
$a = Chr (Hexdec (' E6 ')). Chr (Hexdec (' n ')). Chr (Hexdec (' 9f '));
echo $a;
Can output the sense of Chinese characters, which is why?
Does CHR continue to merge backwards for values exceeding 127?
Reply content:
Chr () in PHP is the conversion between ASCII characters and corresponding numbers.
But why the following code can output Chinese characters?
For example, Chinese characters ' sense ' unt-8 coding is e6849f;
$a = Chr (Hexdec (' E6 ')). Chr (Hexdec (' n ')). Chr (Hexdec (' 9f '));
echo $a;
Can output the sense of Chinese characters, which is why?
Does CHR continue to merge backwards for values exceeding 127?
ASCII codes represent single-byte characters (which include English letters, numbers, punctuation marks, invisible characters, control characters, and so on), which are always less than 0x80, which is less than 128 of the decimal. When the character is processed, if the byte is less than 0x80, it is treated as a single byte, otherwise it will continue to read the next byte, which is usually related to encoding, GBK will treat 2 bytes as one character, UTF8 requires 3 bytes. Sometimes it is necessary to do similar processing in PHP, such as counting the number of characters in a string (the string may contain single-byte and multi-byte), the Strlen method can only calculate the number of bytes, and Mb_strlen needs to open the extension. Such a requirement, in fact, is easy to handle:
function mbstrlen($str){ $len = strlen($str); if ($len <= 0) { return 0; } $count = 0; for ($i = 0; $i < $len; $i++) { $count++; if (ord($str{$i}) >= 0x80) { $i += 2; } } return $count;}