note 19857 deleted from function.utf8-encode by cmb
| From: | cmb@php.net | Date: | Sun, 04 Dec 2016 12:15:49 +0000 |
| Subject: | note 19857 deleted from function.utf8-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208207@lists.php.net to get a copy of this message | ||
Note Submitter: lars(at)ioflux(dot)net
----
This will also do the job for those who're interested:
<?
function utf8toiso8859($string)
{
$returns = "";
$UTF8len = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6);
$pos = 0;
$antal = strlen($string);
do
{
$c = ord($string[$pos]);
$len = $UTF8len[($c >> 2) & 0x3F];
switch ($len)
{
case 6:
$u = $c & 0x01;
break;
case 5:
$u = $c & 0x03;
break;
case 4:
$u = $c & 0x07;
break;
case 3:
$u = $c & 0x0F;
break;
case 2:
$u = $c & 0x1F;
break;
case 1:
$u = $c & 0x7F;
break;
case 0: /* unexpected start of a new character */
$u = $c & 0x3F;
$len = 5;
break;
}
while (--$len && (++$pos < $antal && $c =
ord($string[$pos])))
{
if (($c & 0xC0) == 0x80)
$u = ($u << 6) | ($c & 0x3F);
else
{ /* unexpected start of a new character */
$pos--;
break;
}
}
if ($u <= 0xFF)
$returns .= chr($u);
else
$returns .= '?';
} while (++$pos < $antal);
return $returns;
}
?>