encode/decodeXmlEntities
| From: | James Barwick | Date: | Wed, 19 Nov 2003 15:04:27 +0000 |
| Subject: | encode/decodeXmlEntities | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-23741@lists.php.net to get a copy of this message | ||
Findings to date:
UTF-8...the only thing XMLEntities needs to do is
remove certain characters in the range of 0x00 to 0x20...
ISO8859-xx change certain characters in the range 0x80 to 0xFF with HTML Entities...
I propose the following in the message below:
Before anyone complains that this isn't a patch...it's not ready for a
patch until there is validation from other individuals...not just me.
Any takers want to comment below...if the original developers
of Node.php want to comment, please....
James
/**
* Get the XML Escape table for XML entities.
*
* @param string encoding Type of XML file output encoding
*
* @return mixed table
* @access private
*/
function getTable($outputencoding = 'utf-8') {
// for utf-8
if ($outputencoding == 'utf-8')
return array( chr(0) => '',chr(1) => '',chr(2) => '',chr(3) => '',chr(4) => '',chr(5) => '',chr(6) => '',chr(7) => '',chr(8) => '', chr(11) => '',chr(12) => '',chr(14) => '',chr(15) => '',chr(16) => '',chr(17) => '',chr(18) => '',chr(19) => '', chr(20) => '',chr(21) => '',chr(22) => '',chr(23) => '',chr(24) => '',chr(25) => '',chr(26) => '',chr(27) => '', chr(28) => '',chr(29) => '',chr(30) => '',chr(31) => '','"' => '"','&' => '&','<' => '<','>' => '>');
// Use only for ISO8859-xx character sets...default for now
// if (substr$outputencoding,0,7) == 'ISO8859')
return array(
chr(0) => '',chr(1) => '',chr(2) => '',chr(3) => '',chr(4) => '',chr(5) => '',chr(6) => '',chr(7) => '',chr(8) => '',
chr(11) => '',chr(12) => '',chr(14) => '',chr(15) => '',chr(16) => '',chr(17) => '',chr(18) => '',chr(19) => '',
chr(20) => '',chr(21) => '',chr(22) => '',chr(23) => '',chr(24) => '',chr(25) => '',chr(26) => '',chr(27) => '',
chr(28) => '',chr(29) => '',chr(30) => '',chr(31) => '','"' => '"','&' => '&',
'<' => '<','>' => '>', ' '=>' ', '¡'=>'¡', '¢'=>'¢', '£'=>'£',
'¤'=>'¤', '¥'=>'¥', '¦'=>'¦', '§'=>'§', '¨'=>'¨', '©'=>'©', 'ª'=>'ª',
'«'=>'«', '¬'=>'¬', ''=>'­', '®'=>'®', '¯'=>'¯', '°'=>'°', '±'=>'±', '²'=>'²',
'³'=>'³', '´'=>'´', 'µ'=>'µ', '¶'=>'¶', '·'=>'·', '¸'=>'¸', '¹'=>'¹', 'º'=>'º',
'»'=>'»', '¼'=>'¼', '½'=>'½', '¾'=>'¾','¿'=> '¿',
'À'=> 'À','Á'=> 'Á','Â'=> 'Â','Ã'=> 'Ã','Ä'=> 'Ä','Å'=> 'Å',
'Æ'=> 'Æ','Ç'=> 'Ç','È'=> 'È','É'=> 'É','Ê'=> 'Ê','Ë'=> 'Ë',
'Ì'=> 'Ì','Í'=> 'Í','Î'=> 'Î','Ï'=> 'Ï','Ð'=> 'Ð','Ñ'=> 'Ñ',
'Ò'=> 'Ò','Ó'=> 'Ó','Ô'=> 'Ô','Õ'=> 'Õ','Ö'=> 'Ö','×'=> '×',
'Ø'=> 'Ø','Ù'=> 'Ù','Ú'=> 'Ú','Û'=> 'Û','Ü'=> 'Ü','Ý'=> 'Ý',
'Þ'=> 'Þ','ß'=> 'ß','à'=> 'à','á'=> 'á','â'=> 'â','ã'=> 'ã',
'ä'=> 'ä','å'=> 'å','æ'=> 'æ','ç'=> 'ç','è'=> 'è','é'=> 'é',
'ê'=> 'ê','ë'=> 'ë','ì'=> 'ì','í'=> 'í','î'=> 'î','ï'=> 'ï',
'ð'=> 'ð','ñ'=> 'ñ','ò'=> 'ò','ó'=> 'ó','ô'=> 'ô','õ'=> 'õ',
'ö'=> 'ö','÷'=> '÷','ø'=> 'ø','ù'=> 'ù','ú'=> 'ú','û'=> 'û',
'ü'=> 'ü','ý'=> 'ý','þ'=> 'þ','ÿ'=> 'ÿ');
}
/**
* Escape XML entities.
*
* @param string xml Text string to escape.
* @param string encoding Type of XML file output encoding
*
* @return string xml
* @access public
*/
function encodeXmlEntities($xml,$outputencoding='utf-8')
{
$encoder = getTable($outputencodeing);
$newstr="";
for ($i=0; $i<strlen($xml); $i++)
{
// substring is character safe with mbstring overloading...
$c=substr($xml,$i,1);
if (array_key_exists($c,$encoder))
$newstr.=$encoder[$c];
else
$newstr.=$c;
}
return $newstr;