![]() |
VOOZH | about |
Given a String the task is to convert the given string into ASCII code using PHP. ASCII is the American Standard Code for Information Interchange, which is a widely used standard for encoding characters in computers and digital communication systems.
There are two methods to convert a given String into ASCII code, these are as follows:
Table of Content
The PHP ord() function returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string.
Syntax:
int ord( $string )
Example:
Array ( [0] => 71 [1] => 70 [2] => 71 )
Syntax:
array_map(function($char) {
return ord($char);
}, str_split($str));
Array ( [0] => 71 [1] => 70 [2] => 71 )
The unpack() function in PHP is used to unpack binary string data into an associative array according to the given format. We can use this function to directly obtain the ASCII values of a string.
unpack("C*", $string);Example:
Array ( [1] => 71 [2] => 70 [3] => 71 )