note 35067 added to function.utf8-decode
| From: | morris_hirsch at hotmail dot com | Date: | Mon, 18 Aug 2003 14:43:50 +0000 |
| Subject: | note 35067 added to function.utf8-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54587@lists.php.net to get a copy of this message | ||
Be aware that utf8_decode can not properly store ''wide'' code entities which
have a numeric value too big for a byte.
This function may help you. It converts utf8 to strict ASCII with no high-bit codes, they are all
written as numerics.
// UTF-8 encoding
// bytes bits representation
// 1 7 0bbbbbbb
// 2 11 110bbbbb 10bbbbbb
// 3 16 1110bbbb 10bbbbbb 10bbbbbb
// 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
// Each b represents a bit that can be used to store character data.
// input CANNOT have single byte upper half extended ascii codes
function numeric_entify_utf8 ($utf8_string) {
$out = "";
$ns = strlen ($utf8_string);
for ($nn = 0; $nn < $ns; $nn++) {
$ch = $utf8_string [$nn];
$ii = ord ($ch);
//1 7 0bbbbbbb (127)
if ($ii < 128) $out .= $ch;
//2 11 110bbbbb 10bbbbbb (2047)
else if ($ii >>5 == 6) {
$b1 = ($ii & 31);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b2 = ($ii & 63);
$ii = ($b1 * 64) + $b2;
$ent = sprintf ("&#%d;", $ii);
$out .= $ent;
}
//3 16 1110bbbb 10bbbbbb 10bbbbbb
else if ($ii >>4 == 14) {
$b1 = ($ii & 31);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b2 = ($ii & 63);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b3 = ($ii & 63);
$ii = ((($b1 * 64) + $b2) * 64) + $b3;
$ent = sprintf ("&#%d;", $ii);
$out .= $ent;
}
//4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
else if ($ii >>3 == 30) {
$b1 = ($ii & 31);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b2 = ($ii & 63);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b3 = ($ii & 63);
$nn++;
$ch = $utf8_string [$nn];
$ii = ord ($ch);
$b4 = ($ii & 63);
$ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
$ent = sprintf ("&#%d;", $ii);
$out .= $ent;
}
}
return $out;
}
----
Manual Page -- http://www.php.net/manual/en/function.utf8-decode.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35067
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35067&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35067&report=yes