note 46410 deleted from function.utf8-decode by aidan
| From: | aidan@php.net | Date: | Sun, 10 Oct 2004 07:52:42 +0000 |
| Subject: | note 46410 deleted from function.utf8-decode by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78265@lists.php.net to get a copy of this message | ||
Note Submitter: mrcuongnv at vnu dot edu dot vn
----
I'm making a web in Vietnamese. My DB stores data in UTF-8 format. But I need to convert it no
NCR format and I wrote a function for this problem.
// Convert a string from UTF-8 format to NCR format for all language
// Use first character and up to 5-next characters
// Written by Cuong V. Nguyen, mrcuongnv@vnu.edu.vn
// K46CA, College of Applied Scientific and Technology
// Vietnam National University, Hanoi
function utf8_to_ncr($utf8)
{
$len = strlen($utf8);
$ncr = ""; $i = 0;
while ($i < $len)
{
$code = 0;
$unit = ord($utf8[$i]);
if ($unit < 0x80)
{
$code = $unit;
$next = 0;
}
elseif ($unit < 0xE0)
{
$code = $unit & 0x1F;
$next = 1;
}
elseif ($unit < 0xF0)
{
$code = $unit & 0x0F;
$next = 2;
}
elseif ($unit < 0xF8)
{
$code = $unit & 0x07;
$next = 3;
}
elseif ($unit < 0xFC)
{
$code = $unit & 0x03;
$next = 4;
}
elseif ($unit < 0xFE)
{
$code = $unit & 0x01;
$next = 5;
}
for ($j = 0; $j < $next; $j++)
{
$i++;
$unit = ord($utf8[$i]);
$code = ($code << 6) | ($unit & 0x3F);
}
if ($code < 0x80)
{
$ncr .= chr($code);
}
else
{
$ncr .= "&#" . $code . ";";
}
$i++;
}
return $ncr;
}