note 84612 modified in function.htmlentities by felipe
| From: | felipe@php.net | 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 => '€',
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
--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 => '&#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
http://php.net/manual/en/function.htmlentities.php