note 84612 deleted from function.htmlentities by cmb
| From: | cmb@php.net | Date: | Sun, 01 Dec 2019 08:34:30 +0000 |
| Subject: | note 84612 deleted from function.htmlentities by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213305@lists.php.net to get a copy of this message | ||
Note Submitter: snevi at im dot com dot ve
----
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 => '€',
130 => '‚',
131 => 'ƒ',
132 => '„',
133 => '…',
134 => '†',
135 => '‡',
136 => 'ˆ',
137 => '‰',
138 => 'Š',
139 => '‹',
140 => 'Œ',
142 => 'Ž',
145 => '‘',
146 => '’',
147 => '“',
148 => '”',
149 => '•',
150 => '–',
151 => '—',
152 => '˜',
153 => '™',
154 => 'š',
155 => '›',
156 => 'œ',
158 => 'ž',
159 => 'Ÿ');
$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