note 99984 added to function.htmlentities

From: Date: Fri, 17 Sep 2010 20:14:24 +0000
Subject: note 99984 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-172702@lists.php.net to get a copy of this message
I use this function to encode all the xml entities and also all the &something; that are not defined in xml like &trade; You can also decode what you encode with my decode function. My function works a little like the htmlentities. You can also add other string to the array if you want to exclude them from the encoding. <?php function xml_entity_decode($text, $charset = 'Windows-1252'){ // Double decode, so if the value was &amp;trade; it will become Trademark $text = html_entity_decode($text, ENT_COMPAT, $charset); $text = html_entity_decode($text, ENT_COMPAT, $charset); return $text; } function xml_entities($text, $charset = 'Windows-1252'){ // Debug and Test // $text = "test &amp; &trade; &amp;trade; abc &reg; &amp;reg; &#45;"; // First we encode html characters that are also invalid in xml $text = htmlentities($text, ENT_COMPAT, $charset, false); // XML character entity array from Wiki // Note: &apos; is useless in UTF-8 or in UTF-16 $arr_xml_special_char = array("&quot;","&amp;","&apos;","&lt;","&gt;"); // Building the regex string to exclude all strings with xml special char $arr_xml_special_char_regex = "(?"; foreach($arr_xml_special_char as $key => $value){ $arr_xml_special_char_regex .= "(?!$value)"; } $arr_xml_special_char_regex .= ")"; // Scan the array for &something_not_xml; syntax $pattern = "/$arr_xml_special_char_regex&([a-zA-Z0-9]+;)/"; // Replace the &something_not_xml; with &amp;something_not_xml; $replacement = '&amp;${1}'; return preg_replace($pattern, $replacement, $text); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 216.246.255.116 ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- https://master.php.net/note/edit/99984 Del: integrated -- https://master.php.net/note/delete/99984/integrated Del: useless -- https://master.php.net/note/delete/99984/useless Del: bad code -- https://master.php.net/note/delete/99984/bad+code Del: spam -- https://master.php.net/note/delete/99984/spam Del: non-english -- https://master.php.net/note/delete/99984/non-english Del: in docs -- https://master.php.net/note/delete/99984/in+docs Del: other reasons-- https://master.php.net/note/delete/99984 Reject -- https://master.php.net/note/reject/99984 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172702) next »