note 75120 deleted from function.html-entity-decode by danbrown

From: Date: Tue, 10 Jan 2012 20:12:44 +0000
Subject: note 75120 deleted from function.html-entity-decode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185535@lists.php.net to get a copy of this message
Note Submitter: teecee[(a)]teecee[pont]hu ---- Hi! The main problem with the UTF-8 strings if You try to unhtmlentities them is that the get_html_translation_table() gives back a non-UTF8 conversion table. So the idea is to get the translation table and then translate the needed non-UTF8 strings to UTF8... I have this code working, actually this code is the one sent by 'daviscabral', just with an extra foreach in it ( http://hu.php.net/manual/en/function.htmlentities.php#68479 ) And the code is: <? function unhtmlentitiesUtf8($string) { // replace numeric entities $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string); // replace literal entities $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); // changing translation table to UTF-8 foreach( $trans_tbl as $key => $value ) { $trans_tbl[$key] = iconv( 'ISO-8859-1', 'UTF-8', $value ); } return strtr($string, $trans_tbl); } ?> If You need this in production code, I suggest to get the $trans_tbl into a common-includable file I think it should be faster. ( Maybe the easiest way to do this is to write after the translation: die(var_export($trans_tbl, true)); and copy&paste the source of the displaying text. And don't forget to check if the browser uses UTF8 codepage! ;)

« previous php.notes (#185535) next »