Site icon Nabtron

Convert string variable characters to ASCII codes using PHP

In situations where you want to convert the value stored in your variable to ascii code in PHP you can use the following code.

function nabascii($input) {
foreach (str_split($input) as $obj)
{

$output .= '&#' . ord($obj) . ';';
}
return $output;
}

Call the code by using:
$myvariable = "nabeel";
echo nabascii($myvariable);

This small php code is not only helpful in this particular situation but can be useful in many situations related to converting the value or text stored inside your variable to an ascii code. You can also find out the exact special character being used in that variable by seeing its exact ascii value or you can see if the variable have a carriage return (\r) or a line break (\n), etc. possibilities are limitless! another beautiful code snippet of PHP

Exit mobile version