note 75153 added to function.html-entity-decode
| From: | laurynas dot butkus at gmail dot com | Date: | Tue, 15 May 2007 11:24:31 +0000 |
| Subject: | note 75153 added to function.html-entity-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126604@lists.php.net to get a copy of this message | ||
In PHP4 html_entity_decode() is not working well with UTF-8 spitting: "Warning: cannot yet
handle MBCS in html_entity_decode()!".
This is working solution combining several workarounds:
<?php
function html_entity_decode_utf8($string)
{
static $trans_tbl;
// replace numeric entities
$string = preg_replace('~&#x([0-9a-f]+);~ei',
'code2utf(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);
// replace literal entities
if (!isset($trans_tbl))
{
$trans_tbl = array();
foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
$trans_tbl[$key] = utf8_encode($val);
}
return strtr($string, $trans_tbl);
}
// Returns the utf string corresponding to the unicode value (from php.net, courtesy -
romans@void.lv)
function code2utf($num)
{
if ($num < 128) return chr($num);
if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128)
. chr(($num & 63) + 128);
if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) +
128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
return '';
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 195.14.188.109
----
Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php
Edit -- https://master.php.net/note/edit/75153
Del: integrated -- https://master.php.net/note/delete/75153/integrated
Del: useless -- https://master.php.net/note/delete/75153/useless
Del: bad code -- https://master.php.net/note/delete/75153/bad+code
Del: spam -- https://master.php.net/note/delete/75153/spam
Del: non-english -- https://master.php.net/note/delete/75153/non-english
Del: in docs -- https://master.php.net/note/delete/75153/in+docs
Del: other reasons-- https://master.php.net/note/delete/75153
Reject -- https://master.php.net/note/reject/75153
Search -- https://master.php.net/manage/user-notes.php