Why can the Chr () function output Unicode characters in javascript-php?

Source: Internet
Author: User
Tags control characters
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;}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.