note 50985 added to function.utf8-decode
| From: | (remove) | Date: | Tue, 15 Mar 2005 23:50:40 +0000 |
| Subject: | note 50985 added to function.utf8-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86504@lists.php.net to get a copy of this message | ||
Here is my variant of UTF8 to Cyrillic Win-1251 encoding convertor that replaces all characters but
latin and Russian ones with &#...; entities:
function utf2win1251 ($s)
{
$out = "";
for ($i=0; $i<strlen($s); $i++)
{
$c1 = substr ($s, $i, 1);
$byte1 = ord ($c1);
if ($byte1>>5 == 6) // 110x xxxx, 110 prefix for 2 bytes unicode
{
$i++;
$c2 = substr ($s, $i, 1);
$byte2 = ord ($c2);
$byte1 &= 31; // remove the 3 bit two bytes prefix
$byte2 &= 63; // remove the 2 bit trailing byte prefix
$byte2 |= (($byte1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
$byte1 >>= 2; // c1 shifts 2 to the right
$word = ($byte1<<8) + $byte2;
if ($word==1025) $out .= chr(168); // ¨
elseif ($word==1105) $out .= chr(184); // ¸
elseif ($word>=0x0410 && $word<=0x044F) $out .= chr($word-848); // À-ß à-ÿ
else
{
$a = dechex($byte1);
$a = str_pad($a, 2, "0", STR_PAD_LEFT);
$b = dechex($byte2);
$b = str_pad($b, 2, "0", STR_PAD_LEFT);
$out .= "&#x".$a.$b.";";
}
}
else
{
$out .= $c1;
}
}
return $out;
}
The function is based on 2 other functions posted below.
I hope it will help those who convert UTF8-encoded text to Win-1251 to use it safely on Russian web
pages (works fine in all browsers).
----
Manual Page -- http://www.php.net/manual/en/function.utf8-decode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50985
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50985&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50985&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50985&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50985&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50985&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50985&report=yes
Search -- http://master.php.net/manage/user-notes.php