The ord () function converts a character to a decimal number, and the chr () function converts a decimal number to a character. in binary, octal, the relationship between decimal and hexadecimal forms serves as a bridge between Chinese character encoding. The third part of the study series, the PHP function, 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:
The 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.
The 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:
The 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
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.