note 106535 added to function.htmlentities
| From: | steve at mcdragonsoftware dot com | Date: | Thu, 17 Nov 2011 04:59:36 +0000 |
| Subject: | note 106535 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184536@lists.php.net to get a copy of this message | ||
I'm glad 5.4 has xml support, but many of us are working with older installations, some of us
still have to use PHP4. If you're like me you've been frustrated with trying to use
htmlentites/htmlspecial chars with xml output. I was hoping to find an option to force numeric
encoding, lacking that, I have written my own xmlencode function, which I now offer:
usage:
$string xmlencode( $string )
it will use htmlspecialchars for the valid xml entities amp, quote, lt, gt, (apos) and return the
numeric entity for all other non alpha-numeric characters.
-------------------------------------------
if( !function_exists( 'xmlentities' ) ) {
function xmlentities( $string ) {
$not_in_list = "A-Z0-9a-z\s_-";
return preg_replace_callback( "/[^{$not_in_list}]/" ,
'get_xml_entity_at_index_0' , $string );
}
function get_xml_entity_at_index_0( $CHAR ) {
if( !is_string( $CHAR[0] ) || ( strlen( $CHAR[0] ) > 1 ) ) {
die( "function: 'get_xml_entity_at_index_0' requires data type: 'char'
(single character). '{$CHAR[0]}' does not match this type." );
}
switch( $CHAR[0] ) {
case "'": case '"': case '&': case
'<': case '>':
return htmlspecialchars( $CHAR[0], ENT_QUOTES ); break;
default:
return numeric_entity_4_char($CHAR[0]); break;
}
}
function numeric_entity_4_char( $char ) {
return "&#".str_pad(ord($char), 3, '0', STR_PAD_LEFT).";";
}
}
----
Server IP: 208.69.120.55
Probable Submitter: 67.0.87.214
----
Manual Page -- http://www.php.net/manual/en/function.htmlentities.php
Edit -- https://master.php.net/note/edit/106535
Del: integrated -- https://master.php.net/note/delete/106535/integrated
Del: useless -- https://master.php.net/note/delete/106535/useless
Del: bad code -- https://master.php.net/note/delete/106535/bad+code
Del: spam -- https://master.php.net/note/delete/106535/spam
Del: non-english -- https://master.php.net/note/delete/106535/non-english
Del: in docs -- https://master.php.net/note/delete/106535/in+docs
Del: other reasons-- https://master.php.net/note/delete/106535
Reject -- https://master.php.net/note/reject/106535
Search -- https://master.php.net/manage/user-notes.php