Home >Backend Development >PHP Tutorial >PHP returns the ASCII value of the first character in a string function ord()
Example
Return the ASCII value of "h":
<?php echo ord("h")."<br>"; echo ord("hello")."<br>"; ?>
Definition and usage
ord() function returns the ASCII value of the first character in the string.
Syntax
ord(string)
Parameter Description
string Required. The string from which to obtain the ASCII value.
Technical details
Return value: Returns the ASCII value in integer form.
PHP Version: 4+
Application of ord() function
ord() function is used to return the ASCII value of a character. The most basic usage is to obtain the ASCII value ord of a ('a') returns 97, but in actual development, it is most commonly used in the character interception function to obtain the decimal number of the high and low bit encoding of Chinese characters. For example, for common Chinese character interception functions, please see the PHPWind or Discuz! forums. The principle of the substrs() function or cutstr() function in the source code is to obtain the ASCII code value of the character through the ord() function. If the return value is greater than 127, it is represented as half of the Chinese character, and then the second half is obtained and combined into a complete character. , combined with character encoding such as GBK or UTF-8, etc.
Taking GBK encoding as an example, use the ord() function to judge Chinese characters and return the ASCII value of each Chinese character. The code is as follows
$string = "不要迷恋哥"; $length = strlen($string); var_dump($string);//原始中文 var_dump($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 character String
2, get the length of the variable (number of bytes)
3, print the variable and the length of the variable
4, get each byte value of the variable through the for loop, put the two bytes of a Chinese character Displayed separated by spaces.
The result is as shown below
Illustration: "Don't be obsessed with brother" is 5 Chinese characters, a total of 10 bytes (one Chinese character is 2 bytes), print each one separately The bytes cannot be displayed normally as shown in the figure above
The initial value remains unchanged and the for loop part of the code is modified to display the ASCII value of each byte
$result = array(); for($i=0;$i<$length;$i++){ if(ord($string[$i])>127){ $result[] = ord($string[$i]).' '.ord($string[++$i]); } } var_dump($result);
The above code uses the ord() function to print the ASCII value of each character, the results are as follows
After conversion through the ord() function, the ASCII value of each character can be viewed normally.
The above is the detailed content of PHP returns the ASCII value of the first character in a string function ord(). For more information, please follow other related articles on the PHP Chinese website!