note 100726 rejected from function.htmlentities by nicobn

From: Date: Wed, 03 Nov 2010 21:52:52 +0000
Subject: note 100726 rejected from function.htmlentities by nicobn
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173731@lists.php.net to get a copy of this message
Note Submitter: jan dot curn at gmail dot com ---- This is a simple function that (correctly) escapes XML entities in an UTF-8 string, including non-ASCII characters. Get code for uniord() function from the ord() function man page. <?php function xmlentities( $string ) { $j = mb_strlen( $string, 'UTF-8' ); $result = ""; for( $k = 0; $k < $j; $k++ ) { $char = mb_substr( $string, $k, 1, 'UTF-8' ); switch( $char ) { case "&": $result .= "&amp;"; break; case "\"": $result .= "&quot;"; break; case "<": $result .= "&lt;"; break; case ">": $result .= "&gt;"; break; default: $num = uniord( $char ); if( $num <= 31 || $num == 39 // "'" || $num == 96 // "`" || $num >= 127 ) $result .= "&#".$num.";"; else $result .= $char; break; } } return $result; } ?>

« previous php.notes (#173731) next »