PHP : UTF-8 to Hex String and Hex/Decimal Array

Today I spent a lot time to find a suitable library to convert UTF-8 text (specially Bangla) to hexadecimal values. After spending few hours with other online libraries, I have decided to write my own 🙂 these are pretty handy functions if you are using MPDF to create PDF with UTF-8 strings , specially in Bangla.

 

function utf8StringToHexString($string) {
$nums = array();
$convmap = array(0x0, 0xffff, 0, 0xffff);
$strlen = mb_strlen($string, “UTF-8”);
for ($i = 0; $i < $strlen; $i++) {
$ch = mb_substr($string, $i, 1, “UTF-8”);
$decimal = substr(mb_encode_numericentity($ch, $convmap, ‘UTF-8’), -5, 4);
$nums[] = “&#x” .base_convert($decimal, 10, 16). “;”;
}
return implode(“”, $nums);
}

function utf8StringToHexArray($string) {
$nums = array();
$convmap = array(0x0, 0xffff, 0, 0xffff);
$strlen = mb_strlen($string, “UTF-8”);
for ($i = 0; $i < $strlen; $i++) {
$ch = mb_substr($string, $i, 1, “UTF-8”);
$decimal = substr(mb_encode_numericentity($ch, $convmap, ‘UTF-8’), -5, 4);
$nums[] = “&#x” .base_convert($decimal, 10, 16). “;”;
}
return $nums;
}

function utf8StringToDecimalArray($string) {
$nums = array();
$convmap = array(0x0, 0xffff, 0, 0xffff);
$strlen = mb_strlen($string, “UTF-8”);
for ($i = 0; $i < $strlen; $i++) {
$ch = mb_substr($string, $i, 1, “UTF-8”);
$nums[] = mb_encode_numericentity($ch, $convmap, ‘UTF-8’);
}
return $nums;
}

Downloading PHP 6.0 from CVS :)

just downloaded the php 6.0 files from php.net cvs . i am really curious to see what’s going on with the new development. It is expected that the unicode support will be improved a lot in this version of php. Going to build it and have a look with the new version.

updates to be followed.