How to use the ord function to get the ASCII code of a character in PHP

王林
Release: 2023-06-26 19:16:01
Original
1839 people have browsed it

Using the ord function to obtain the ASCII code of a character in PHP is a very basic operation, and it is also one of the very important knowledge points for developers who are new to the PHP language. This article will introduce how to use the ord function to obtain the ASCII code of a character.

1. Introduction to ord function

The ord function is a built-in function in PHP, which is used to obtain the ASCII code of a character. The syntax of this function is as follows:

int ord ( string $string )
Copy after login

Among them, $string is the character whose ASCII code is to be obtained, and the return value is the ASCII code value of the character (0~255).

2. Use of ord function

The following uses several examples to demonstrate how to use the ord function to obtain the ASCII code of a character.

  1. Get the ASCII code of the character

Code example:

<?php
    $str = "A";
    $ascii = ord($str);
    echo "字符 $str 的ASCII码为 $ascii";
?>
Copy after login

Output result:

字符 A 的ASCII码为 65
Copy after login

Code description:

In the above code, we use the ord function to obtain the ASCII code of the character "A", and then output the result through the echo statement.

  1. Get the ASCII code of each character in the string

Code example:

<?php
    $str = "hello, world!";
    for ($i = 0; $i < strlen($str); $i++) {
        $ascii = ord($str[$i]);
        echo "字符 $str[$i] 的ASCII码为 $ascii <br/>";
    }
?>
Copy after login

Output result:

字符 h 的ASCII码为 104
字符 e 的ASCII码为 101
字符 l 的ASCII码为 108
字符 l 的ASCII码为 108
字符 o 的ASCII码为 111
字符 , 的ASCII码为 44
字符   的ASCII码为 32
字符 w 的ASCII码为 119
字符 o 的ASCII码为 111
字符 r 的ASCII码为 114
字符 l 的ASCII码为 108
字符 d 的ASCII码为 100
字符 ! 的ASCII码为 33
Copy after login

Code description :

In the above code, we traverse each character in the string through a for loop, then use the ord function to obtain the ASCII code of each character, and finally output the result through the echo statement.

  1. Processing of other character encodings

In some cases, the characters in the string may not be ASCII encoded. For example, when using Chinese characters, you need to use character sets such as UTF-8. In this case, the value obtained by the ord function may not be the ASCII code value we expect. At this time, other functions need to be used for character encoding conversion. Here is an example:

Code example:

<?php
    $str = "你好";
    $utf8 = iconv("GB2312", "UTF-8", $str);
    for ($i = 0; $i < strlen($utf8); $i++) {
        $ascii = ord($utf8[$i]);
        echo "字符 $utf8[$i] 的ASCII码为 $ascii <br/>";
    }
?>
Copy after login

Output result:

字符 你 的ASCII码为 228
字符 好 的ASCII码为 189
字符 好 的ASCII码为 173
Copy after login

Code description:

In the above example, we first use iconv The function converts the string from GB2312 encoding to UTF-8 encoding, and then uses the ord function to obtain the ASCII code of each character. Note that when performing character encoding conversion, you need to make a selection based on the actual situation.

3. Summary

Using the ord function to obtain the ASCII code of a character is a very basic operation and is very practical in PHP programming. This article demonstrates through several examples how to use the ord function to obtain the ASCII code of a character. I hope it will be helpful to beginners.

The above is the detailed content of How to use the ord function to get the ASCII code of a character in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!