Can someone suggest a (preferably) graceful way to convert an ASCII character to its decimal equivalent using PHP?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
function ascii_to_dec($str)
{
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
$dec_array[] = ord($str{$i});
}
return $dec_array;
}
example usage :
$ascii ="\t";
print_r( ascii_to_dec($ascii));
returns an array
Array
(
[0] => 9
)
回答2:
ord()
is what you need
回答3:
ord() returns the integer ascii value of a character
chr() returns a character from an ascii value
回答4:
Try ord.
回答5:
Just to add onto streetparade's answer
foreach ($array as $decval)
{
echo $decval;
}
returns raw dec for the characters.