note 68536 deleted from function.html-entity-decode by danbrown
| From: | danbrown@php.net | Date: | Tue, 10 Jan 2012 20:13:21 +0000 |
| Subject: | note 68536 deleted from function.html-entity-decode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185540@lists.php.net to get a copy of this message | ||
Note Submitter: derernst at gmx dot ch
----
Combining the suggestions by buraks78 at gmail dot com, gaui at gaui dot is, daniel at brightbyte
dot de, and the version in PEAR_PHP_Compat, I come to the following, which should work in an UTF-8
environment, with PHP < or > 4.3:
<?php
function decode_entities($text, $quote_style = ENT_COMPAT) {
if (function_exists('html_entity_decode')) {
$text = html_entity_decode($text, $quote_style, 'ISO-8859-1'); // NOTE: UTF-8 does not
work!
}
else {
$trans_tbl = get_html_translation_table(HTML_ENTITIES, $quote_style);
$trans_tbl = array_flip($trans_tbl);
$text = strtr($text, $trans_tbl);
}
$text = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))',
$text);
$text = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $text);
return $text;
}
?>
Note that I omitted the line
$trans_table['''] = "'";
as it would override the quote_style setting and thus lead to unexpected results for quote_styles
ENT_NOQUOTES and ENT_COMPAT.