note 75120 added to function.html-entity-decode

From: Date: Sun, 13 May 2007 15:29:29 +0000
Subject: note 75120 added to function.html-entity-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-126479@lists.php.net to get a copy of this message
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! ;) ---- Server IP: 195.70.37.52 Probable Submitter: 84.0.45.219 ---- Manual Page -- http://www.php.net/manual/en/function.html-entity-decode.php Edit -- https://master.php.net/note/edit/75120 Del: integrated -- https://master.php.net/note/delete/75120/integrated Del: useless -- https://master.php.net/note/delete/75120/useless Del: bad code -- https://master.php.net/note/delete/75120/bad+code Del: spam -- https://master.php.net/note/delete/75120/spam Del: non-english -- https://master.php.net/note/delete/75120/non-english Del: in docs -- https://master.php.net/note/delete/75120/in+docs Del: other reasons-- https://master.php.net/note/delete/75120 Reject -- https://master.php.net/note/reject/75120 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126479) next »