note 100726 added to function.htmlentities
| From: | jan dot curn at gmail dot com | Date: | Tue, 02 Nov 2010 23:58:14 +0000 |
| Subject: | note 100726 added to function.htmlentities | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173701@lists.php.net to get a copy of this message | ||
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 .= "&"; break;
case "\"": $result .= """; break;
case "<": $result .= "<"; break;
case ">": $result .= ">"; break;
default:
$num = uniord( $char );
if( $num <= 31
|| $num == 39 // "'"
|| $num == 96 // "`"
|| $num >= 127 )
$result .= "&#".$num.";";
else
$result .= $char;
break;
}
}
return $result;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.100.157.91
----
Manual Page -- http://www.php.net/manual/en/function.htmlentities.php
Edit -- https://master.php.net/note/edit/100726
Del: integrated -- https://master.php.net/note/delete/100726/integrated
Del: useless -- https://master.php.net/note/delete/100726/useless
Del: bad code -- https://master.php.net/note/delete/100726/bad+code
Del: spam -- https://master.php.net/note/delete/100726/spam
Del: non-english -- https://master.php.net/note/delete/100726/non-english
Del: in docs -- https://master.php.net/note/delete/100726/in+docs
Del: other reasons-- https://master.php.net/note/delete/100726
Reject -- https://master.php.net/note/reject/100726
Search -- https://master.php.net/manage/user-notes.php