John Davidson

convert ASCII value to a character based on specific char in php

0 comments
Message:


I'd like to ask dumb question.
How could I convert @48@49@50 based on @ char ?
In PHP, understand that chr() function is used to convert a ASCII value to a character.
48 is 0.
49 is 1.
And 50 is 2.
May I know how to convert @48@49@50 as 012 and how to store 012 in one variable ?
Eg- $num = 012



My Answer:
You can achieve this by first converting the ASCII values to characters using the `chr()` function and then concatenating them together. Here's an example code snippet to demonstrate this:

php
// ASCII values
$ascii_values = "@48@49@50";

// Split the ASCII values by '@' character
$ascii_array = explode('@', $ascii_values);

// Remove the empty first element
array_shift($ascii_array);

// Convert ASCII values to characters and concatenate them
$converted_string = '';
foreach ($ascii_array as $ascii) {
$converted_string .= chr($ascii);
}

// Store the concatenated string in a variable
$num = $converted_string;

// Output the result
echo $num; // Output: 012


In this code snippet, we first split the ASCII values by the '@' character and then convert each ASCII value to a character using the `chr()` function. Finally, we concatenate the characters together and store the result in the `$num` variable.

Rate this post

5 of 5 based on 5005 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us