Measure the test taker's knowledge about the application of the ord () and chr () functions in PHP.

Source: Internet
Author: User
Tags php basics

The third part of the Chinese character encoding Study Series, the PHP function section, master the application of the ord () and chr () functions, in the previous [PHP Basics], I learned about ASCII code and character conversion methods, but two special functions are required between character conversion, used for conversion between characters and decimal digits. The ord () function converts characters to decimal digits. the chr () function converts decimal digits into characters in binary, octal, it serves as a bridge between decimal and hexadecimal.

I. Application of ord () Functions
The ord () function is used to return the ASCII value of a character. The most basic usage is to obtain the ASCII value of a. ord ('A') and return 97. However, in actual development, the most widely used character truncation function is used to obtain the decimal number of Chinese Characters in high and low bit encoding. For example, you can refer to PHPWind or Discuz for common Chinese Character truncation functions! In the source code of the forum, the substrs () function or cutstr () function is used to obtain the ASCII value of a character through the ord () function. If the return value is greater than 127, it is half of the Chinese character, then get the last half of the combination into a complete character, combined with character encoding such as GBK or UTF-8.

Using GBK encoding as an example, the ord () function is used to determine whether Chinese characters return the ASCII values of each Chinese character. The Code is as follows:
Copy codeThe Code is as follows:
$ String = "Do not be infatuated with Brother ";
$ Length = strlen ($ string );
Var_dump ($ string); // original Chinese
Var_dump ($ length); // length
$ Result = array ();
For ($ I = 0; $ I <$ length; $ I ++ ){
If (ord ($ string [$ I])> 127 ){
$ Result [] = $ string [$ I]. ''. $ string [++ $ I];
}
}
Var_dump ($ result );

Code Description
1. Define a variable $ string whose value is a string
2. Get the variable length (bytes)
3. Print the length of variables and variables
4. Get the values of each byte of the variable through the for loop, and separate the two bytes of a Chinese character with spaces.
The result is as follows:

Illustration: "Do not be infatuated with Brother" is five Chinese characters, a total of 10 bytes (one Chinese Character 2 bytes), print each byte cannot be normally displayed as shown in

The initial value remains unchanged. Some codes in the for loop display the ASCII values of each byte.
Copy codeThe Code is as follows:
$ Result = array ();
For ($ I = 0; $ I <$ length; $ I ++ ){
If (ord ($ string [$ I])> 127 ){
$ Result [] = ord ($ string [$ I]). ''. ord ($ string [++ $ I]);
}
}
Var_dump ($ result );

The code above uses the ord () function to print the ASCII values of each character. The result is as follows:

After converting the ord () function, you can view the ASCII values of each character.

2. Application of the chr () function

The function chr () is opposite to the function ord (). It is used to return the specified character. For example, chr (97) returns.

In combination with the above example, as long as you get the ASCII value of Chinese characters, you can use the chr () function to assemble Chinese characters. The Code is as follows:
Copy codeThe Code is as follows:
$ String = "Do not be infatuated with Brother ";
$ Length = strlen ($ string );
Var_dump ($ string); // original Chinese
Var_dump ($ length); // length
$ Result = array ();
For ($ I = 0; $ I <$ length; $ I ++ ){
If (ord ($ string [$ I])> 127 ){
$ Result [] = ord ($ string [$ I]). ''. ord ($ string [++ $ I]);
}
}
Var_dump ($ result );
Foreach ($ result as $ v ){
$ Decs = explode ("", $ v );
Echo chr ($ decs [0]). chr ($ decs [1]);
}

The result is as follows:


The above Code does not directly output Chinese characters, but prints normal Chinese characters. The principle is to first obtain the ASCII value of each byte and convert it to byte through the chr () function, then combine the two bytes to form a complete Chinese character.

Through the discussion of the ord () and chr () functions, we have initially understood the encoding principles of Chinese characters, learned about a Chinese character in GBK encoding, and used ord () and chr () the function implements the conversion of each byte. Please pay attention to the conversion principle of Chinese character encoding in the next series.

References
Performance Comparison Between the substrs and cutstr functions of PHPWind and Discuz truncation character Functions

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.