How to use unpack() Function In PHP
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.
Syntax:
unpack("C*", $string);
Example:
<?php
$str = "GFG";
$asciiArr = unpack("C*", $str);
print_r($asciiArr);
?>
Output
Array ( [1] => 71 [2] => 70 [3] => 71 )
PHP Program to Convert String to ASCII Value
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
- Using ord() Function and for() Loop
- Using array_map(), ord() and str_split() Methods