note 106192 deleted from function.htmlspecialchars by danbrown
| From: | danbrown@php.net | Date: | Tue, 18 Oct 2011 14:46:57 +0000 |
| Subject: | note 106192 deleted from function.htmlspecialchars by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183970@lists.php.net to get a copy of this message | ||
Note Submitter: steve at mcdragonsoftware dot com
----
@ me
okay, I missed the part where it said these constants were added in PHP 5.4 . Chances are I'm
not the only one working with multiple installations of PHP and multiple versions. A lot of what I
do has to stay compatible with PHP4 due to legacy systems clients are running. Anyway, here is a
wrapper function for xml encoding... if will use the ENT_XML1 encoding if available ( PHP >= 5.4
) and run a double encode (htmlspecialchars to escape the ampersand on illegal xml entities
generated by htmlentities).
( a far more desirable approach would be to use &#xxx; format encoding, but I see no option thus
)
function xml_safe( $text ) {
//double encoding because htmlentities is NOT SAFE FOR XML in PHP < 5.4
if( floatval(phpversion()) < 5.4 ) {
$encoded = htmlspecialchars( htmlentities( $text, ENT_QUOTES ), ENT_QUOTES );
$encoded = str_replace(
array( '&amp;', '&quot;', '&#039;',
'&lt;', '&gt;' ),
array( '&', '"', ''',
'<', '>' ),
$encoded
);
return $encoded;
} else {
return htmlentities( $text, ENT_QUOTES | ENT_XML1 );
}
}