encode/decodeXmlEntities

From: 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) => '','"' => '"','&' => '&',
'<' => '&lt;','>' => '&gt;');
    // 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) => '','"' => '&quot;','&' => '&amp;', '<' => '&lt;','>' => '&gt;', ' '=>'&nbsp;', '¡'=>'&iexcl;', '¢'=>'&cent;', '£'=>'&pound;', '¤'=>'&curren;', '¥'=>'&yen;', '¦'=>'&brvbar;', '§'=>'&sect;', '¨'=>'&uml;', '©'=>'&copy;', 'ª'=>'&ordf;', '«'=>'&laquo;', '¬'=>'&not;', '­'=>'&shy;', '®'=>'&reg;', '¯'=>'&macr;', '°'=>'&deg;', '±'=>'&plusmn;', '²'=>'&sup2;', '³'=>'&sup3;', '´'=>'&acute;', 'µ'=>'&micro;', '¶'=>'&para;', '·'=>'&middot;', '¸'=>'&cedil;', '¹'=>'&sup1;', 'º'=>'&ordm;', '»'=>'&raquo;', '¼'=>'&frac14;', '½'=>'&frac12;', '¾'=>'&frac34;','¿'=> '&iquest;', 'À'=> '&Agrave;','Á'=> '&Aacute;','Â'=> '&Acirc;','Ã'=> '&Atilde;','Ä'=> '&Auml;','Å'=> '&Aring;', 'Æ'=> '&AElig;','Ç'=> '&Ccedil;','È'=> '&Egrave;','É'=> '&Eacute;','Ê'=> '&Ecirc;','Ë'=> '&Euml;', 'Ì'=> '&Igrave;','Í'=> '&Iacute;','Î'=> '&Icirc;','Ï'=> '&Iuml;','Ð'=> '&ETH;','Ñ'=> '&Ntilde;', 'Ò'=> '&Ograve;','Ó'=> '&Oacute;','Ô'=> '&Ocirc;','Õ'=> '&Otilde;','Ö'=> '&Ouml;','×'=> '&times;', 'Ø'=> '&Oslash;','Ù'=> '&Ugrave;','Ú'=> '&Uacute;','Û'=> '&Ucirc;','Ü'=> '&Uuml;','Ý'=> '&Yacute;', 'Þ'=> '&THORN;','ß'=> '&szlig;','à'=> '&agrave;','á'=> '&aacute;','â'=> '&acirc;','ã'=> '&atilde;', 'ä'=> '&auml;','å'=> '&aring;','æ'=> '&aelig;','ç'=> '&ccedil;','è'=> '&egrave;','é'=> '&eacute;', 'ê'=> '&ecirc;','ë'=> '&euml;','ì'=> '&igrave;','í'=> '&iacute;','î'=> '&icirc;','ï'=> '&iuml;', 'ð'=> '&eth;','ñ'=> '&ntilde;','ò'=> '&ograve;','ó'=> '&oacute;','ô'=> '&ocirc;','õ'=> '&otilde;', 'ö'=> '&ouml;','÷'=> '&divide;','ø'=> '&oslash;','ù'=> '&ugrave;','ú'=> '&uacute;','û'=> '&ucirc;', 'ü'=> '&uuml;','ý'=> '&yacute;','þ'=> '&thorn;','ÿ'=> '&yuml;'); }
    /**
    * 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;


« previous php.pear.dev (#23741) next »