View Full Version : How the character code in PHP
softgroups
Sep 18th 2007, 1:19 pm
I known i can get the ASCII code of an character using ord() but i want to encode a string like %45,%20, so i need the HEX code or however it's called
sea otter
Sep 18th 2007, 8:13 pm
rawurlencode() (http://us3.php.net/manual/en/function.rawurlencode.php)
softgroups
Sep 19th 2007, 4:02 am
that do not encrypt the string, only the special characters from the string!
nico_swd
Sep 19th 2007, 4:19 am
This should do what you want.
function str_to_hex($string)
{
$hex = '';
$len = strlen($string);
for ($i = 0; $i < $len; $i++)
{
$hex .= '%' . str_pad(dechex(ord($string[$i])), 2, 0, STR_PAD_LEFT);
}
return $hex;
}
softgroups
Sep 21st 2007, 7:20 am
One more question , how i can do the reverse of this!?
sea otter
Sep 21st 2007, 7:39 pm
In keeping with nico's scheme of things :)
function hex_to_str($hex)
{
$string = '';
$data = explode('%',$hex);
$len = count($data);
for ($i=1; $i < $len; $i++) // yes, ONE. skip first array element, which is empty
$string .= chr(hexdec($data[$i]));
return $string;
}
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.