note 47371 added to function.html-entity-decode

From: Date: Sun, 14 Nov 2004 02:12:00 +0000
Subject: note 47371 added to function.html-entity-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-80479@lists.php.net to get a copy of this message
This function seems to have to have two limitations (at least in PHP 4.3.8): a) it does not work with multibyte character codings, such as UTF-8 b) it does not decode numeric entity references a) can be solved by using iconv to convert to ISO-8859-1, then decoding the entities, than convert to UTF-8 again. But that's quite ugly and detroys all characters not present in Latin-1. b) can be solved rather nicely using the following code: <?php function decode_entities($text) { $text= html_entity_decode($text,ENT_QUOTES,"ISO-8859-1"); #NOTE: UTF-8 does not work! $text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); #decimal notation $text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text); #hex notation return $text; } ?> HTH ---- Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47371 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47371&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47371&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80479) next »