note 68536 added to function.html-entity-decode

From: Date: Tue, 01 Aug 2006 10:09:25 +0000
Subject: note 68536 added to function.html-entity-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-115405@lists.php.net to get a copy of this message
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['&#39;'] = "'"; as it would override the quote_style setting and thus lead to unexpected results for quote_styles ENT_NOQUOTES and ENT_COMPAT. ---- Server IP: 194.153.94.10 Probable Submitter: 213.180.171.13 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php Edit -- https://master.php.net/note/edit/68536 Del: integrated -- https://master.php.net/note/delete/68536/integrated Del: useless -- https://master.php.net/note/delete/68536/useless Del: bad code -- https://master.php.net/note/delete/68536/bad+code Del: spam -- https://master.php.net/note/delete/68536/spam Del: non-english -- https://master.php.net/note/delete/68536/non-english Del: in docs -- https://master.php.net/note/delete/68536/in+docs Del: other reasons-- https://master.php.net/note/delete/68536 Reject -- https://master.php.net/note/reject/68536 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115405) next »