note 35067 deleted from function.utf8-decode by cmb
| From: | cmb@php.net | Date: | Sun, 11 Dec 2016 11:34:21 +0000 |
| Subject: | note 35067 deleted from function.utf8-decode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208535@lists.php.net to get a copy of this message | ||
Note Submitter: morris_hirsch at hotmail dot com
----
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;
}