Sunday, March 22, 2009

php - decutf8 decimal to utf8 entity

go to http://www.zedwood.com/unicode_table you will see a unicode table listed by htmlentity number.

echo decutf8(632);//prints ɸ which has htmlentity ɸ

function decutf8($str)
{
$bin = decbin($str);
$out = 127;
if (strlen($bin)<=7)
{
$bin=sprintf("%08d", $bin);
$out=bindec($bin);
}
else if (strlen($bin)<=11)
{
$bin=sprintf("%011d", $bin);
$out=bindec("110".substr($bin,0,5)."10".substr($bin,5,6));
}
else if (strlen($bin)<=16)
{
$bin=sprintf("%016d", $bin);
$out=bindec("1110".substr($bin,0,4)."10".substr($bin,4,6)."10".substr($bin,4,6));
}
else if (strlen($bin)<=21)
{
$bin=sprintf("%021d", $bin);
$out=bindec("11110".substr($bin,0,3)."10".substr($bin,3,6)."10".substr($bin,9,6)."10".substr($bin,15,6));
}

if ($out <=255)
return chr($out);
else if ($out <=(255 * 256) )
return chr($out/256).chr($out%256);
else if ($out <=(255 * 256 * 256) )
return chr($out/256).chr($out/256).chr($out%256);
else if ($out <=(255 * 256 * 256 * 256) )
return chr($out/256).chr($out/256).chr($out/256).chr($out%256);
}

No comments: