note 84612 modified in function.htmlentities by felipe

From: Date: Tue, 22 Jul 2008 11:55:32 +0000
Subject: note 84612 modified in function.htmlentities by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142488@lists.php.net to get a copy of this message
correction to my previous post and improvement of the function: (the post was changed by the html parser and the characters displays as they should not) <?php function XMLEntities($string) { $string = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '_privateXMLEntities("$0")', $string); return $string; } function _privateXMLEntities($num) { $chars = array( 128 => '&#8364;', 130 => '&#8218;', 131 => '&#402;', 132 => '&#8222;', 133 => '&#8230;', 134 => '&#8224;', 135 => '&#8225;', 136 => '&#710;', 137 => '&#8240;', 138 => '&#352;', 139 => '&#8249;', 140 => '&#338;', 142 => '&#381;', 145 => '&#8216;', 146 => '&#8217;', 147 => '&#8220;', 148 => '&#8221;', 149 => '&#8226;', 150 => '&#8211;', 151 => '&#8212;', 152 => '&#732;', 153 => '&#8482;', 154 => '&#353;', 155 => '&#8250;', 156 => '&#339;', 158 => '&#382;', 159 => '&#376;'); $num = ord($num); return (($num > 127 && $num < 160) ? $chars[$num] : "&#".$num.";" ); } ?> in the previous post, to correct the HEX values that are not rendered, the program use a for each cicle, but that introduces a mayor complexity in execution time, so, we use the ability to call functions in the preg_replace second parameter, and ceate another funcion that evaluates the ord of the character given, and if it is between 127 and 160 it returns the modified HEX value to be understood by the browser and not brake the XML (this work with dynamic XML generated form php with dynamic data from any source) p.d: the '&'(&) should appear in this post as a single ampersand character and not as the html entity --was-- correction to my previous post and improvement of the function: (the post was changed by the html parser and the characters displays as they should not) function XMLEntities($string) { $string = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '_privateXMLEntities("$0")', $string); return $string; } function _privateXMLEntities($num) { $chars = array( 128 => '&amp;#8364;', 130 => '&amp;#8218;', 131 => '&amp;#402;', 132 => '&amp;#8222;', 133 => '&amp;#8230;', 134 => '&amp;#8224;', 135 => '&amp;#8225;', 136 => '&amp;#710;', 137 => '&amp;#8240;', 138 => '&amp;#352;', 139 => '&amp;#8249;', 140 => '&amp;#338;', 142 => '&amp;#381;', 145 => '&amp;#8216;', 146 => '&amp;#8217;', 147 => '&amp;#8220;', 148 => '&amp;#8221;', 149 => '&amp;#8226;', 150 => '&amp;#8211;', 151 => '&amp;#8212;', 152 => '&amp;#732;', 153 => '&amp;#8482;', 154 => '&amp;#353;', 155 => '&amp;#8250;', 156 => '&amp;#339;', 158 => '&amp;#382;', 159 => '&amp;#376;'); $num = ord($num); return (($num > 127 && $num < 160) ? $chars[$num] : "&amp;#".$num.";" ); } in the previous post, to correct the HEX values that are not rendered, the program use a for each cicle, but that introduces a mayor complexity in execution time, so, we use the ability to call functions in the preg_replace second parameter, and ceate another funcion that evaluates the ord of the character given, and if it is between 127 and 160 it returns the modified HEX value to be understood by the browser and not brake the XML (this work with dynamic XML generated form php with dynamic data from any source) p.d: the '&amp;'(&) should appear in this post as a single ampersand character and not as the html entity http://php.net/manual/en/function.htmlentities.php

« previous php.notes (#142488) next »