note 106535 modified in function.htmlentities by thiago

From: Date: Tue, 29 Nov 2011 13:21:57 +0000
Subject: note 106535 modified in function.htmlentities by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184777@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. ------------------------------------------- <?php 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).";"; } } ?> --was-- 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).";"; } } http://php.net/manual/en/function.htmlentities.php

« previous php.notes (#184777) next »