note 27426 deleted from function.utf8-decode by cmb
| From: | cmb@php.net | Date: | Sun, 11 Dec 2016 11:32:15 +0000 |
| Subject: | note 27426 deleted from function.utf8-decode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208530@lists.php.net to get a copy of this message | ||
Note Submitter: wielspm at xs4all dot nl
----
Here is a function I made to convert all 2 byte utf in a &#xxx; form:
function utf2html ($utf2html_string)
{
$utf2html_retstr = "";
for ($utf2html_p=0; $utf2html_p<strlen($utf2html_string); $utf2html_p++):
$utf2html_c = substr ($utf2html_string, $utf2html_p, 1);
$utf2html_c1 = ord ($utf2html_c);
if ($utf2html_c1>>5 == 6): // 110x xxxx, 110 prefix for 2 bytes unicode
$utf2html_p++;
$utf2html_t = substr ($utf2html_string, $utf2html_p, 1);
$utf2html_c2 = ord ($utf2html_t);
$utf2html_c1 &= 31; // remove the 3 bit two bytes prefix
$utf2html_c2 &= 63; // remove the 2 bit trailing byte prefix
$utf2html_c2 |= (($utf2html_c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
$utf2html_c1 >>= 2; // c1 shifts 2 to the right
$utf2html_n = dechex($utf2html_c1).dechex($utf2html_c2);
$utf2html_retstr .= sprintf ("&#%03d;", hexdec($utf2html_n));
else:
$utf2html_retstr .= $utf2html_c;
endif;
endfor;
return $utf2html_retstr;
}