note 106192 added to function.htmlspecialchars
| From: | steve at mcdragonsoftware dot com | Date: | Tue, 18 Oct 2011 06:44:50 +0000 |
| Subject: | note 106192 added to function.htmlspecialchars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183947@lists.php.net to get a copy of this message | ||
@ 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 );
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 97.123.208.62
----
Manual Page -- http://www.php.net/manual/en/function.htmlspecialchars.php
Edit -- https://master.php.net/note/edit/106192
Del: integrated -- https://master.php.net/note/delete/106192/integrated
Del: useless -- https://master.php.net/note/delete/106192/useless
Del: bad code -- https://master.php.net/note/delete/106192/bad+code
Del: spam -- https://master.php.net/note/delete/106192/spam
Del: non-english -- https://master.php.net/note/delete/106192/non-english
Del: in docs -- https://master.php.net/note/delete/106192/in+docs
Del: other reasons-- https://master.php.net/note/delete/106192
Reject -- https://master.php.net/note/reject/106192
Search -- https://master.php.net/manage/user-notes.php