PHP: Use CHR to print Chinese characters

Source: Internet
Author: User
Use chr($i)The ASCII can be printed smoothly, however, when $i>=19968 (the Unicode starting value of the Chinese character is 4E00 decimal), it is not possible to print out the characters.
Such as:


  
   

Although there are other methods, such as:

//代码2$character = html_entity_decode('一', ENT_QUOTES, 'UTF-8');

But why doesn't code 1 print out Chinese characters?

Reply content:

The use chr($i) can print out the ASCII smoothly, however, when $i>=19968 (the Unicode starting value of the Chinese character 4E00 decimal), found unable to print out the Chinese characters.
Such as:


  
   

Although there are other methods, such as:

//代码2$character = html_entity_decode('一', ENT_QUOTES, 'UTF-8');

But why doesn't code 1 print out Chinese characters?

ASCII does not contain Chinese characters at all,
It is gb2312-80,gbk,big5,unicode that contains Chinese characters.
Chinese characters are multibyte, and you'll find out with Ord that only the first byte of the kanji will be returned.

In fact, the source of the CHR function is/ext/standard/string.c
The code is as follows

PHP_FUNCTION(chr)

{
Long C;
Char temp[2];

if (ZEND_NUM_ARGS() != 1) {    WRONG_PARAM_COUNT;}if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "l", &c) == FAILURE) {    c = 0;}temp[0] = (char)c;temp[1] = '\0';RETURN_STRINGL(temp, 1, 1);

}
As you can see, it is the character cast of the C language.

  • 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.