note 84221 added to function.get-html-translation-table

From: Date: Thu, 03 Jul 2008 19:47:21 +0000
Subject: note 84221 added to function.get-html-translation-table
Groups: php.notes 
Request: Send a blank email to php-notes+get-141127@lists.php.net to get a copy of this message
"rafael at phpit dot com dot br" your solution only works for the ISO-8859-1 encoding, I mean, it works but only for that encoding and that's because get_html_translation_table won't let you specify the charset... it uses the default one, that is ISO-8859-1 The solution from "olito24 at gmx dot de" does work for UTF-8, I just modified it a bit specifying the UTF-8 charset, also the $str parameter wasn't being used at all, I just renamed it to $string Note: Change ENT_NOQUOTES to ENT_QUOTES to convert both double and single quotes These are the functions to encode html but tags using UTF-8 and ISO-8859-1 <?php class Html { /*by olito24 at gmx dot de*/ function htmlButTags($string) { $pattern = '<([a-zA-Z0-9\. "\'_\/-=;\(\)?&#%]+)>'; preg_match_all ('/' . $pattern . '/', $string, $tagMatches, PREG_SET_ORDER); $textMatches = preg_split ('/' . $pattern . '/', $string); foreach ($textMatches as $key => $value) { $textMatches [$key] = htmlentities ($value, ENT_NOQUOTES, 'UTF-8'); } for ($i = 0; $i < count ($textMatches); $i ++) { $textMatches [$i] = $textMatches [$i] . $tagMatches [$i] [0]; } return implode ($textMatches); } /*by "rafael at phpit dot com dot br" */ function htmlButTags_iso($str){ // Take all the html entities $caracteres = get_html_translation_table(HTML_ENTITIES,ENT_NOQUOTES); // Find out the "tags" entities $remover = get_html_translation_table(HTML_SPECIALCHARS,ENT_NOQUOTES); // Spit out the tags entities from the original table $caracteres = array_diff($caracteres, $remover); // Translate the string.... $str = strtr($str, $caracteres); // And that's it! return $str; } } ?> ---- Server IP: 80.64.47.13 Probable Submitter: 89.129.148.153 ---- Manual Page -- http://www.php.net/manual/en/function.get-html-translation-table.php Edit -- https://master.php.net/note/edit/84221 Del: integrated -- https://master.php.net/note/delete/84221/integrated Del: useless -- https://master.php.net/note/delete/84221/useless Del: bad code -- https://master.php.net/note/delete/84221/bad+code Del: spam -- https://master.php.net/note/delete/84221/spam Del: non-english -- https://master.php.net/note/delete/84221/non-english Del: in docs -- https://master.php.net/note/delete/84221/in+docs Del: other reasons-- https://master.php.net/note/delete/84221 Reject -- https://master.php.net/note/reject/84221 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141127) next »