Sai A Sai A
Updated date Aug 16, 2023
In this blog, we will explore how to convert string to ASCII conversion in PHP. Learn multiple methods, including built-in functions and loops, to convert strings into their corresponding ASCII values.

Introduction:

Dealing with strings and characters is an essential task. The main operation is converting a string into its corresponding ASCII values. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents characters using numeric codes. In this blog, we will learn various methods of converting a string to ASCII in PHP

Method 1: Using the ord() Function

The simplest and most direct way to convert a string to its ASCII representation in PHP is by utilizing the built-in ord() function. This function takes a single character (the first character of the input string) and returns its ASCII value.

$inputString = "Hello, World!";
$firstCharacter = $inputString[0];
$asciiValue = ord($firstCharacter);
echo "ASCII value of '$firstCharacter' is: $asciiValue";

Output:

ASCII value of 'H' is: 72

The ord() function takes the first character of the input string, which in this case is 'H', and converts it into its ASCII value, which is 72. This method is straightforward and suitable for cases where you need to convert individual characters.

Method 2: Using a Loop

If you want to convert an entire string to its ASCII representation, you can use a loop to iterate through each character and apply the ord() function.

$inputString = "Hello";
$asciiArray = [];

for ($i = 0; $i < strlen($inputString); $i++) {
    $asciiArray[] = ord($inputString[$i]);
}

echo "ASCII values of '$inputString': " . implode(', ', $asciiArray);

Output:

ASCII values of 'Hello': 72, 101, 108, 108, 111

In this method, we loop through each character of the input string using a for loop. The ord() function is applied to each character, and the resulting ASCII values are stored in an array. Finally, the implode() function is used to join the ASCII values with commas and display the result.

Method 3: Using array_map() and Anonymous Function

For a more concise and functional approach, you can use the array_map() function along with an anonymous function to convert each character into its ASCII value.

$inputString = "World";
$asciiArray = array_map(function($char) {
    return ord($char);
}, str_split($inputString));

echo "ASCII values of '$inputString': " . implode(', ', $asciiArray);

Output:

ASCII values of 'World': 87, 111, 114, 108, 100

In this method, str_split() is used to split the input string into an array of characters. The array_map() function is then applied to each character, using the anonymous function to convert it into its ASCII value.

Conclusion:

In this blog, we have learned three distinct methods for achieving this conversion in PHP. The first method, using the ord() function, is optimal for single-character conversions. The second method, utilizing a loop, allows for the conversion of entire strings. The third method, employing array_map() and an anonymous function, provides a more concise and functional approach.

Comments (0)

There are no comments. Be the first to comment!!!