cvs: pear /HTML_Entities Entities.php package.xml /HTML_Entities/Entities Exception.php /HTML_Entities/examples html_entities_example.php /HTML_Entities/tests html_entities1.phpt html_entities2.phpt html_entities3.phpt
[email protected] ("Charles Brunet")
| Newsgroups | php.pear.cvs |
|---|---|
| Message-ID | <cvscbrunet1210693660@cvsserver> |
cbrunet Tue May 13 15:47:40 2008 UTC
Added files:
/pear/HTML_Entities Entities.php package.xml
/pear/HTML_Entities/Entities Exception.php
/pear/HTML_Entities/examples html_entities_example.php
/pear/HTML_Entities/tests html_entities1.phpt html_entities2.phpt
html_entities3.phpt
Log:
Initial release
cbrunet-20080513154740.txt
(text/plain, 45.5 KB)
http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/Entities.php?view=markup&rev=1.1 Index: pear/HTML_Entities/Entities.php +++ pear/HTML_Entities/Entities.php <?php /* vim: set noai expandtab ts=4 st=4 sw=4: */ /** * Convert utf8 (or other text encoding) to HTML entities, and vice-versa * * PHP versions 5 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * The names of its contributors may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_Entities * @author Charles Brunet <[email protected]> * @license http://www.opensource.org/licenses/bsd-license.html BSD License * @version CVS: $Id: Entities.php,v 1.1 2008/05/13 15:47:39 cbrunet Exp $ * @link http://pear.php.net/package/HTML_Entities */ require_once 'HTML/Entities/Exception.php'; /** * Convert utf8 (or other text encoding) to HTML entities, and vice-versa * * All functions are static. Functions use utf8 encoding. It uses * utf8_encode and utf8_decode functions to deal with latin1 (iso-8859-1) * and iconv (if available) to deal with other charsets. * * The default behavior is to encode utf8 strings using named entities * compatible with HTML 4.0, and to decode all entities. * * Examples: * * $myencodedtext = HTML_Entities::encode($mytext); * $mytextagain = HTML_Entities::decode($myencodedtext); * * To encode using numerical entities: * $myencodedtext = HTML_Entities::encode($mytext, HTML_Entities::CODES) * * To encode HTML, without touching HTML tags: * $myencodedtext = HTML_Entities::encode($mytext, HTML_Entities::NAMES, * HTML_Entities::HTML40 | HTML_Entities::IGNORE_SPECIAL_CHARS) * * To decode to latin1: * HTML_Entities::decode($mytext, HTML_Entities::ALL, 'latin1'); * * The list of entities was taken here: * http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references * * @category HTML * @package HTML_Entities * @author Charles Brunet <[email protected]> * @license http://www.opensource.org/licenses/bsd-license.html BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_Entities */ class HTML_Entities { /** * Convert special chars to named entities. */ const NAMES = 0x01; /** * Convert special chars to code entities. */ const CODES = 0x02; /** * Entities defined in HTML 2.0 */ const HTML20 = 0x01; /** * Entities defined in HTML 3.2 */ const HTML32 = 0x02; /** * Entities defined in HTML 4.0 */ const HTML40 = 0x04; /** * Entities defined in XML */ const XML = 0x10; /** * What to do with illegal chars during conversions. * * If we are in XML, then illegal chars are x00-x08 x0b x0c x0e-x1f * xd800-xdfff xfffe xffff x110000 and greater. * * Otherwise, illegal characters can be encountered when converting * to other charsets. */ const IGNORE_ILLEGAL_CHARS = 0x00; const DROP_ILLEGAL_CHARS = 0x80; const EXCEPTION_ON_ILLEGAL_CHAR = 0xf0; /** * Entities defined in XHTML 1.0 */ const XHTML10 = 0x14; // self::HTML40 | self::XML; /** * Do not convert < > " ' & */ const IGNORE_SPECIAL_CHARS = 0x40; /** * Convert ALL entities */ const ALL = 0x3f; /** * Encode $text with HTML entities. * * @param string $text The text to encode * @param int $what What kind of encoding to use (names and/or codes) * @param int $compat Compatibility mode (HTML 2.0, 3.2, 4.0, XHTML 1.0) * @param string $charset Charset of the input string * * @return string The encoded text * @access public * @static */ public static function encode($text, $what = self::NAMES, $compat = self::HTML40, $charset = 'utf-8') { // Convert $text to utf-8 switch ($charset) { case 'utf-8': case 'utf8': case 'UTF-8': case 'UTF8': // Nothing to do $charset = 'utf-8'; break; case 'iso-8859-1': case 'latin-1': case 'latin1': case 'ISO-8859-1': $charset = 'iso-8859-1'; $text = utf8_encode($text); break; default: if (function_exists('iconv')) { if ($compat & self::DROP_ILLEGAL_CHARS) { $outset = 'UTF-8//IGNORE'; } else { $outset = 'UTF-8//TRANSLIT'; } $text = iconv($charset, $outset, $text); if ($test === false) { throw new HTML_Entities_Exception('HTML_Entities: iconv conversion error'); } } else { throw new HTML_Entities_Exception('HTML_Entities: iconv function not available.'); } break; } if ($what & self::NAMES) { $t = self::getTranslationTable($compat); $text = strtr($text, $t); } if ($what & self::CODES) { // Remplace remaining chars with numerical entities if ($compat & self::XML) { if (!($compat & self::NAMES)) { $search_str = '/[\x22\x26\x27\x3c\x3e]/'; } else { $search_str = null; } } else { $search_str = '/['; if ($compat & self::IGNORE_SPECIAL_CHARS) { $search_str .= ''; } else { $search_str .= '\x22\x27\x3c\x3e'; if (!($what & self::NAMES)) { // replace & only if we dont have named entities. $search_str .= '\x26'; } $search_str .= ']|'; } $search_str .= '[\xc0-\xdf][\x80-\xbf]|'. '[\xe0-\xef][\x80-\xbf][\x80-\xbf]|'. '[\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/'; } if (!is_null($search_str)) { $text = preg_replace_callback($search_str, 'HTML_Entities::_utf8ord', $text); } } else if ($charset == 'iso-8859-1') { $text = utf8_decode($text); } else if ($charset != 'utf-8') { $text = iconv('utf-8', $charset, $text); } return $text; } /** * Return unicode numerical entity from a utf8 char. * * @param array $matches The array returned by preg_replace. Indice 0 is * the utf8 char to convert. * * @static * @access private * @return string HTML numerical entity. */ private static function _utf8ord($matches) { $str = $matches[0]; // $str can be 1, 2, 3 or 4 bytes long switch (strlen($str)) { case 2: $y = ord($str[0]) & 0x1f; $z = ord($str[1]) & 0x3f; $c = dechex(($y << 6) + $z); break; case 3: $x = ord($str[0]) & 0x0f; $y = ord($str[1]) & 0x3f; $z = ord($str[2]) & 0x3f; $c = dechex(($x << 12) + ($y << 6) + $z); break; case 4: $w = ord($str[0]) & 0x07; $x = ord($str[1]) & 0x3f; $y = ord($str[2]) & 0x3f; $z = ord($str[3]) & 0x3f; $c = dechex(($w << 18) + ($x << 12) + ($y << 6) + $z); if (strlen($c) == 5) { // It is best to have a even number of digits. $c = "0".$c; } default: $c = dechex(ord($str[0])); } // It is best to have 4 digits minimum. $c = str_pad($c, 4, "0", STR_PAD_LEFT); return "&#x".$c.";"; } /** * Decode $text * * @param string $text The text to decode * @param int $options Compatibility mode (HTML 2.0, 3.2, 4.0, XHTML 1.0) * @param string $charset Charset of the output * * @return string The encoded text * @access public * @static */ public static function decode($text, $options = self::ALL, $charset = 'utf-8') { // Convert numerical entities $text = preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', 'HTML_Entities::_utf8chr', $text); // Handle illegal chars... if ($options & self::XML) { if ($options & self::DROP_ILLEGAL_CHARS) { // illegal characters in XML $text = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]|'. '[\xed][\xa0-\xbf][\x80-\xbf]|'. '[\xed][\xef][\xbe\xbf]/', '', $text); } elseif ($options & self::EXCEPTION_ON_ILLEGAL_CHAR) { if (preg_match('/[\x00-\x08\x0b\x0c\x0e-\x1f]|'. '[\xed][\xa0-\xbf][\x80-\xbf]|'. '[\xed][\xef][\xbe\xbf]/', $text)) { throw new HTML_Entities_Exception('HTML_Entities: illegal character in XML'); } } } // Convert named entities $t = self::getTranslationTable($options); $t = array_flip($t); $text = strtr($text, $t); switch ($charset) { case 'utf-8': case 'utf8': case 'UTF-8': case 'UTF8': // Nothing to do break; case 'iso-8859-1': case 'latin-1': case 'latin1': case 'ISO-8859-1': $text = utf8_decode($text); break; default: if (function_exists('iconv')) { $text = iconv('utf-8', $charset, $text); } else { throw new HTML_Entities_Exception('HTML_Entities: iconv function not available.'); } break; } return $text; } /** * Convert html nemurical entity to utf8 char. * * @param array $matches Array returned from preg_replace. The indice * 1 contains the code, in decimal or hexadecimal. * * @static * @access private * @return string An utf8 char. */ private static function _utf8chr($matches) { $c = $matches[1]; if ($c[0] == "x") { // If we have an hexadecimal number, convert it. $c = substr($c, 1); $c = ltrim($c, '0'); $c = hexdec($c); } else { $c = intval($c); } // convert unicode to utf8 if ($c < 0x80) { $s = chr($c); } elseif ($c < 0x0800) { $y = ($c & 0x07c0) >> 6; $z = ($c & 0x003f); $s = chr(0xc0 | $y).chr(0x80 | $z); } elseif ($c < 0x010000) { $x = ($c & 0xf000) >> 12; $y = ($c & 0x0fc0) >> 6; $z = ($c & 0x003f); $s = chr(0xe0 | $x).chr(0x80 | $y).chr(0x80 | $z); } else { $w = ($c & 0x1c0000) >> 18; $x = ($c & 0x03f000) >> 12; $y = ($c & 0x000fc0) >> 6; $z = ($c & 0x00003f); $s = chr(0xf0 | $w).chr(0x80 | $x).chr(0x80 | $y). chr(0x80 | $z); } return $s; } /** * Get the translation table. * * @param int $options Compatibility mode. * * @return array The array of named entities, associated with utf8 chars. * @access public * @static */ public static function getTranslationTable($options = self::ALL) { $a = array(); if ($options & self::HTML40) { $options |= self::HTML32; } if ($options & self::HTML32) { $options |= self::HTML20; } if ($options & self::HTML20) { $a = array_merge($a, array( "\xc3\x80" => "À", "\xc3\x81" => "Á", "\xc3\x82" => "Â", "\xc3\x83" => "Ã", "\xc3\x84" => "Ä", "\xc3\x85" => "Å", "\xc3\x86" => "Æ", "\xc3\x87" => "Ç", "\xc3\x88" => "È", "\xc3\x89" => "É", "\xc3\x8a" => "Ê", "\xc3\x8b" => "Ë", "\xc3\x8c" => "Ì", "\xc3\x8d" => "Í", "\xc3\x8e" => "Î", "\xc3\x8f" => "Ï", "\xc3\x90" => "Ð", "\xc3\x91" => "Ñ", "\xc3\x92" => "Ò", "\xc3\x93" => "Ó", "\xc3\x94" => "Ô", "\xc3\x95" => "Õ", "\xc3\x96" => "Ö", "\xc3\x98" => "Ø", "\xc3\x99" => "Ù", "\xc3\x9a" => "Ú", "\xc3\x9b" => "Û", "\xc3\x9c" => "Ü", "\xc3\x9d" => "Ý", "\xc3\x9e" => "Þ", "\xc3\x9f" => "ß", "\xc3\xa0" => "à", "\xc3\xa1" => "á", "\xc3\xa2" => "â", "\xc3\xa3" => "ã", "\xc3\xa4" => "ä", "\xc3\xa5" => "å", "\xc3\xa6" => "æ", "\xc3\xa7" => "ç", "\xc3\xa8" => "è", "\xc3\xa9" => "é", "\xc3\xaa" => "ê", "\xc3\xab" => "ë", "\xc3\xac" => "ì", "\xc3\xad" => "í", "\xc3\xae" => "î", "\xc3\xaf" => "ï", "\xc3\xb0" => "ð", "\xc3\xb1" => "ñ", "\xc3\xb2" => "ò", "\xc3\xb3" => "ó", "\xc3\xb4" => "ô", "\xc3\xb5" => "õ", "\xc3\xb6" => "ö", "\xc3\xb8" => "ø", "\xc3\xb9" => "ù", "\xc3\xba" => "ú", "\xc3\xbb" => "û", "\xc3\xbc" => "ü", "\xc3\xbd" => "ý", "\xc3\xbe" => "þ", "\xc3\xbf" => "ÿ", )); } if ($options & self::HTML32) { $a = array_merge($a, array( "\xc2\xa0" => " ", "\xc2\xa1" => "¡", "\xc2\xa2" => "¢", "\xc2\xa3" => "£", "\xc2\xa4" => "¤", "\xc2\xa5" => "¥", "\xc2\xa6" => "¦", "\xc2\xa7" => "§", "\xc2\xa8" => "¨", "\xc2\xa9" => "©", "\xc2\xaa" => "ª", "\xc2\xab" => "«", "\xc2\xac" => "¬", "\xc2\xad" => "­", "\xc2\xae" => "®", "\xc2\xaf" => "¯", "\xc2\xb0" => "°", "\xc2\xb1" => "±", "\xc2\xb2" => "²", "\xc2\xb3" => "³", "\xc2\xb4" => "´", "\xc2\xb5" => "µ", "\xc2\xb6" => "¶", "\xc2\xb7" => "·", "\xc2\xb8" => "¸", "\xc2\xb9" => "¹", "\xc2\xba" => "º", "\xc2\xbb" => "»", "\xc2\xbc" => "¼", "\xc2\xbd" => "½", "\xc2\xbe" => "¾", "\xc2\xbf" => "¿", "\xc3\x97" => "×", "\xc3\xb7" => "÷", )); } if ($options & self::HTML40) { $a = array_merge($a, array( "\xc5\x92" => "Œ", "\xc5\x93" => "œ", "\xc5\xa0" => "Š", "\xc5\xa1" => "š", "\xc5\xb8" => "Ÿ", "\xc6\x92" => "ƒ", "\xcb\x86" => "ˆ", "\xcb\x9c" => "˜", "\xce\x91" => "Α", "\xce\x92" => "Β", "\xce\x93" => "Γ", "\xce\x94" => "Δ", "\xce\x95" => "Ε", "\xce\x96" => "Ζ", "\xce\x97" => "Η", "\xce\x98" => "Θ", "\xce\x99" => "Ι", "\xce\x9a" => "Κ", "\xce\x9b" => "Λ", "\xce\x9c" => "Μ", "\xce\x9d" => "Ν", "\xce\x9e" => "Ξ", "\xce\x9f" => "Ο", "\xce\xa0" => "Π", "\xce\xa1" => "Ρ", "\xce\xa3" => "Σ", "\xce\xa4" => "Τ", "\xce\xa5" => "Υ", "\xce\xa6" => "Φ", "\xce\xa7" => "Χ", "\xce\xa8" => "Ψ", "\xce\xa9" => "Ω", "\xce\xb1" => "α", "\xce\xb2" => "β", "\xce\xb3" => "γ", "\xce\xb4" => "δ", "\xce\xb5" => "ε", "\xce\xb6" => "ζ", "\xce\xb7" => "η", "\xce\xb8" => "θ", "\xce\xb9" => "ι", "\xce\xba" => "κ", "\xce\xbb" => "λ", "\xce\xbc" => "μ", "\xce\xbd" => "ν", "\xce\xbe" => "ξ", "\xce\xbf" => "ο", "\xcf\x80" => "π", "\xcf\x81" => "ρ", "\xcf\x82" => "ς", "\xcf\x83" => "σ", "\xcf\x84" => "τ", "\xcf\x85" => "υ", "\xcf\x86" => "φ", "\xcf\x87" => "χ", "\xcf\x88" => "ψ", "\xcf\x89" => "ω", "\xcf\x91" => "ϑ", "\xcf\x92" => "ϒ", "\xcf\x96" => "ϖ", "\xe2\x80\x82" => " ", "\xe2\x80\x83" => " ", "\xe2\x80\x89" => " ", "\xe2\x80\x8c" => "‌", "\xe2\x80\x8d" => "‍", "\xe2\x80\x8e" => "‎", "\xe2\x80\x8f" => "‏", "\xe2\x80\x93" => "–", "\xe2\x80\x94" => "—", "\xe2\x80\x98" => "‘", "\xe2\x80\x99" => "’", "\xe2\x80\x9a" => "‚", "\xe2\x80\x9c" => "“", "\xe2\x80\x9d" => "”", "\xe2\x80\x9e" => "„", "\xe2\x80\xa0" => "†", "\xe2\x80\xa1" => "‡", "\xe2\x80\xa2" => "•", "\xe2\x80\xa6" => "…", "\xe2\x80\xb0" => "‰", "\xe2\x80\xb2" => "′", "\xe2\x80\xb3" => "″", "\xe2\x80\xb9" => "‹", "\xe2\x80\xba" => "›", "\xe2\x80\xbe" => "‾", "\xe2\x81\x84" => "⁄", "\xe2\x82\xac" => "€", "\xe2\x84\x91" => "ℑ", "\xe2\x84\x98" => "℘", "\xe2\x84\x9c" => "ℜ", "\xe2\x84\xa2" => "™", "\xe2\x84\xb5" => "ℵ", "\xe2\x86\x90" => "←", "\xe2\x86\x91" => "↑", "\xe2\x86\x92" => "→", "\xe2\x86\x93" => "↓", "\xe2\x86\x94" => "↔", "\xe2\x86\xb5" => "↵", "\xe2\x87\x90" => "⇐", "\xe2\x87\x91" => "⇑", "\xe2\x87\x92" => "⇒", "\xe2\x87\x93" => "⇓", "\xe2\x87\x94" => "⇔", "\xe2\x88\x80" => "∀", "\xe2\x88\x82" => "∂", "\xe2\x88\x83" => "∃", "\xe2\x88\x85" => "∅", "\xe2\x88\x87" => "∇", "\xe2\x88\x88" => "∈", "\xe2\x88\x89" => "∉", "\xe2\x88\x8b" => "∋", "\xe2\x88\x8f" => "∏", "\xe2\x88\x91" => "∑", "\xe2\x88\x92" => "−", "\xe2\x88\x97" => "∗", "\xe2\x88\x9a" => "√", "\xe2\x88\x9d" => "∝", "\xe2\x88\x9e" => "∞", "\xe2\x88\xa0" => "∠", "\xe2\x88\xa7" => "∧", "\xe2\x88\xa8" => "∨", "\xe2\x88\xa9" => "∩", "\xe2\x88\xaa" => "∪", "\xe2\x88\xab" => "∫", "\xe2\x88\xb4" => "∴", "\xe2\x88\xbc" => "∼", "\xe2\x89\x85" => "≅", "\xe2\x89\x88" => "≈", "\xe2\x89\xa0" => "≠", "\xe2\x89\xa1" => "≡", "\xe2\x89\xa4" => "≤", "\xe2\x89\xa5" => "≥", "\xe2\x8a\x82" => "⊂", "\xe2\x8a\x83" => "⊃", "\xe2\x8a\x84" => "⊄", "\xe2\x8a\x86" => "⊆", "\xe2\x8a\x87" => "⊇", "\xe2\x8a\x95" => "⊕", "\xe2\x8a\x97" => "⊗", "\xe2\x8a\xa5" => "⊥", "\xe2\x8b\x85" => "⋅", "\xe2\x8c\x88" => "⌈", "\xe2\x8c\x89" => "⌉", "\xe2\x8c\x8a" => "⌊", "\xe2\x8c\x8b" => "⌋", "\xe2\x8c\xa9" => "⟨", "\xe2\x8c\xaa" => "⟩", "\xe2\x97\x8a" => "◊", "\xe2\x99\xa0" => "♠", "\xe2\x99\xa3" => "♣", "\xe2\x99\xa5" => "♥", "\xe2\x99\xa6" => "♦", )); } if (!($options & self::IGNORE_SPECIAL_CHARS)) { $a = array_merge($a, array( "\x22" => """, "\x26" => "&", "\x3c" => "<", "\x3e" => ">", )); if ($options & self::XML) { $a = array_merge($a, array( "\x27" => "'", )); } } return $a; } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/package.xml?view=markup&rev=1.1 Index: pear/HTML_Entities/package.xml +++ pear/HTML_Entities/package.xml <?xml version="1.0" encoding="UTF-8"?> <package packagerversion="1.7.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>HTML_Entities</name> <channel>pear.php.net</channel> <summary>Convert text to/from HTML entities.</summary> <description>This package allo to convert utf-8 text to html entities and vice-versa. It provides functions similar to php functions html_entity_decode, htmlentities and get_html_translation_table, but it works properly with utf-8 in PHP4, and contains ALL defined HTML entities. </description> <lead> <name>Charles Brunet</name> <user>cbrunet</user> <email>[email protected]</email> <active>yes</active> </lead> <date>2008-05-13</date> <time>11:14:34</time> <version> <release>0.2.2</release> <api>0.2.2</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://www.opensource.org/licenses/bsd-license.html"> BSD License</license> <notes>This is the first release. </notes> <contents> <dir name="/" baseinstalldir="HTML"> <file name="Entities.php" role="php"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> <dir name="Entities"> <file name="Exception.php" role="php"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> </dir> <dir name="tests"> <file name="html_entities1.phpt" role="test"/> <file name="html_entities2.phpt" role="test"/> <file name="html_entities3.phpt" role="test"/> </dir> <dir name="examples"> <file name="html_entities_example.php" role="doc"/> </dir> </dir> </contents> <dependencies> <required> <php> <min>5.0.0</min> </php> <pearinstaller> <min>1.4.0b1</min> </pearinstaller> </required> </dependencies> <phprelease /> </package> http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/Entities/Exception.php?view=markup&rev=1.1 Index: pear/HTML_Entities/Entities/Exception.php +++ pear/HTML_Entities/Entities/Exception.php <?php /* vim: set noai expandtab ts=4 st=4 sw=4: */ /** * Exception class used by HTML_Entities package. * * PHP versions 5 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * The names of its contributors may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_Entities * @author Charles Brunet <[email protected]> * @license http://www.opensource.org/licenses/bsd-license.html BSD License * @version CVS: $Id: Exception.php,v 1.1 2008/05/13 15:47:39 cbrunet Exp $ * @link http://pear.php.net/package/HTML_Entities */ require_once "PEAR/Exception.php"; /** * Exeption class for the HTML_Entities package. * * @category HTML * @package HTML_Entities * @author Charles Brunet <[email protected]> * @license http://www.opensource.org/licenses/bsd-license.html BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_Entities */ class HTML_Entities_Exception extends PEAR_Exception { } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/examples/html_entities_example.php?view=markup&rev=1.1 Index: pear/HTML_Entities/examples/html_entities_example.php +++ pear/HTML_Entities/examples/html_entities_example.php <html> <head><title>HTML_Entities example</title></head> <body> <?php error_reporting(E_STRICT); ini_set('display_errors', true); require_once "HTML/Entities.php"; // 1. Define a text (utf8) we want to convert to entities: $t = "Le français est une langue qui nécessite des caractères spéciaux."; // 2. Convert it to HTML entities: $t = HTML_Entities::encode($t); echo "<p>".$t."</p>"; // 3. Convert it back to latin1: try { $t = HTML_Entities::decode($t, HTML_Entities::ALL, "latin1"); } catch (PEAR_Exception $e) { var_dump($e); } echo "<p>".$t."</p>"; // 4. Write the list of entities echo "<table>"; $table = HTML_Entities::getTranslationTable(); foreach ($table as $char=>$entity) { echo "<tr><td>{$char}</td><td>"; echo HTML_Entities::encode($entity); echo "</td></tr>"; } echo "</table>"; ?> </body></html> http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/tests/html_entities1.phpt?view=markup&rev=1.1 Index: pear/HTML_Entities/tests/html_entities1.phpt +++ pear/HTML_Entities/tests/html_entities1.phpt --TEST-- HTML_Entities::getTranslationTable test --FILE-- <?php require_once "HTML/Entities.php"; var_dump(HTML_Entities::getTranslationTable()); ?> --EXPECT-- array(253) { ["À"]=> string(8) "À" ["Á"]=> string(8) "Á" ["Â"]=> string(7) "Â" ["Ã"]=> string(8) "Ã" ["Ä"]=> string(6) "Ä" ["Å"]=> string(7) "Å" ["Æ"]=> string(7) "Æ" ["Ç"]=> string(8) "Ç" ["È"]=> string(8) "È" ["É"]=> string(8) "É" ["Ê"]=> string(7) "Ê" ["Ë"]=> string(6) "Ë" ["Ì"]=> string(8) "Ì" ["Í"]=> string(8) "Í" ["Î"]=> string(7) "Î" ["Ï"]=> string(6) "Ï" ["Ð"]=> string(5) "Ð" ["Ñ"]=> string(8) "Ñ" ["Ò"]=> string(8) "Ò" ["Ó"]=> string(8) "Ó" ["Ô"]=> string(7) "Ô" ["Õ"]=> string(8) "Õ" ["Ö"]=> string(6) "Ö" ["Ø"]=> string(8) "Ø" ["Ù"]=> string(8) "Ù" ["Ú"]=> string(8) "Ú" ["Û"]=> string(7) "Û" ["Ü"]=> string(6) "Ü" ["Ý"]=> string(8) "Ý" ["Þ"]=> string(7) "Þ" ["ß"]=> string(7) "ß" ["à"]=> string(8) "à" ["á"]=> string(8) "á" ["â"]=> string(7) "â" ["ã"]=> string(8) "ã" ["ä"]=> string(6) "ä" ["å"]=> string(7) "å" ["æ"]=> string(7) "æ" ["ç"]=> string(8) "ç" ["è"]=> string(8) "è" ["é"]=> string(8) "é" ["ê"]=> string(7) "ê" ["ë"]=> string(6) "ë" ["ì"]=> string(8) "ì" ["í"]=> string(8) "í" ["î"]=> string(7) "î" ["ï"]=> string(6) "ï" ["ð"]=> string(5) "ð" ["ñ"]=> string(8) "ñ" ["ò"]=> string(8) "ò" ["ó"]=> string(8) "ó" ["ô"]=> string(7) "ô" ["õ"]=> string(8) "õ" ["ö"]=> string(6) "ö" ["ø"]=> string(8) "ø" ["ù"]=> string(8) "ù" ["ú"]=> string(8) "ú" ["û"]=> string(7) "û" ["ü"]=> string(6) "ü" ["ý"]=> string(8) "ý" ["þ"]=> string(7) "þ" ["ÿ"]=> string(6) "ÿ" [" "]=> string(6) " " ["¡"]=> string(7) "¡" ["¢"]=> string(6) "¢" ["£"]=> string(7) "£" ["¤"]=> string(8) "¤" ["¥"]=> string(5) "¥" ["¦"]=> string(8) "¦" ["§"]=> string(6) "§" ["¨"]=> string(5) "¨" ["©"]=> string(6) "©" ["ª"]=> string(6) "ª" ["«"]=> string(7) "«" ["¬"]=> string(5) "¬" [""]=> string(5) "­" ["®"]=> string(5) "®" ["¯"]=> string(6) "¯" ["°"]=> string(5) "°" ["±"]=> string(8) "±" ["²"]=> string(6) "²" ["³"]=> string(6) "³" ["´"]=> string(7) "´" ["µ"]=> string(7) "µ" ["¶"]=> string(6) "¶" ["·"]=> string(8) "·" ["¸"]=> string(7) "¸" ["¹"]=> string(6) "¹" ["º"]=> string(6) "º" ["»"]=> string(7) "»" ["¼"]=> string(8) "¼" ["½"]=> string(8) "½" ["¾"]=> string(8) "¾" ["¿"]=> string(8) "¿" ["×"]=> string(7) "×" ["÷"]=> string(8) "÷" ["Œ"]=> string(7) "Œ" ["œ"]=> string(7) "œ" ["Š"]=> string(8) "Š" ["š"]=> string(8) "š" ["Ÿ"]=> string(6) "Ÿ" ["ƒ"]=> string(6) "ƒ" ["ˆ"]=> string(6) "ˆ" ["˜"]=> string(7) "˜" ["Α"]=> string(7) "Α" ["Β"]=> string(6) "Β" ["Γ"]=> string(7) "Γ" ["Δ"]=> string(7) "Δ" ["Ε"]=> string(9) "Ε" ["Ζ"]=> string(6) "Ζ" ["Η"]=> string(5) "Η" ["Θ"]=> string(7) "Θ" ["Ι"]=> string(6) "Ι" ["Κ"]=> string(7) "Κ" ["Λ"]=> string(8) "Λ" ["Μ"]=> string(4) "Μ" ["Ν"]=> string(4) "Ν" ["Ξ"]=> string(4) "Ξ" ["Ο"]=> string(9) "Ο" ["Π"]=> string(4) "Π" ["Ρ"]=> string(5) "Ρ" ["Σ"]=> string(7) "Σ" ["Τ"]=> string(5) "Τ" ["Υ"]=> string(9) "Υ" ["Φ"]=> string(5) "Φ" ["Χ"]=> string(5) "Χ" ["Ψ"]=> string(5) "Ψ" ["Ω"]=> string(7) "Ω" ["α"]=> string(7) "α" ["β"]=> string(6) "β" ["γ"]=> string(7) "γ" ["δ"]=> string(7) "δ" ["ε"]=> string(9) "ε" ["ζ"]=> string(6) "ζ" ["η"]=> string(5) "η" ["θ"]=> string(7) "θ" ["ι"]=> string(6) "ι" ["κ"]=> string(7) "κ" ["λ"]=> string(8) "λ" ["μ"]=> string(4) "μ" ["ν"]=> string(4) "ν" ["ξ"]=> string(4) "ξ" ["ο"]=> string(9) "ο" ["π"]=> string(4) "π" ["ρ"]=> string(5) "ρ" ["ς"]=> string(8) "ς" ["σ"]=> string(7) "σ" ["τ"]=> string(5) "τ" ["υ"]=> string(9) "υ" ["φ"]=> string(5) "φ" ["χ"]=> string(5) "χ" ["ψ"]=> string(5) "ψ" ["ω"]=> string(7) "ω" ["ϑ"]=> string(10) "ϑ" ["ϒ"]=> string(7) "ϒ" ["ϖ"]=> string(5) "ϖ" [" "]=> string(6) " " [" "]=> string(6) " " [" "]=> string(8) " " [""]=> string(6) "‌" [""]=> string(5) "‍" [""]=> string(5) "‎" [""]=> string(5) "‏" ["–"]=> string(7) "–" ["—"]=> string(7) "—" ["‘"]=> string(7) "‘" ["’"]=> string(7) "’" ["‚"]=> string(7) "‚" ["“"]=> string(7) "“" ["”"]=> string(7) "”" ["„"]=> string(7) "„" ["†"]=> string(8) "†" ["‡"]=> string(8) "‡" ["•"]=> string(6) "•" ["…"]=> string(8) "…" ["‰"]=> string(8) "‰" ["′"]=> string(7) "′" ["″"]=> string(7) "″" ["‹"]=> string(8) "‹" ["›"]=> string(8) "›" ["‾"]=> string(7) "‾" ["⁄"]=> string(7) "⁄" ["€"]=> string(6) "€" ["ℑ"]=> string(7) "ℑ" ["℘"]=> string(8) "℘" ["ℜ"]=> string(6) "ℜ" ["™"]=> string(7) "™" ["ℵ"]=> string(9) "ℵ" ["←"]=> string(6) "←" ["↑"]=> string(6) "↑" ["→"]=> string(6) "→" ["↓"]=> string(6) "↓" ["↔"]=> string(6) "↔" ["↵"]=> string(7) "↵" ["⇐"]=> string(6) "⇐" ["⇑"]=> string(6) "⇑" ["⇒"]=> string(6) "⇒" ["⇓"]=> string(6) "⇓" ["⇔"]=> string(6) "⇔" ["∀"]=> string(8) "∀" ["∂"]=> string(6) "∂" ["∃"]=> string(7) "∃" ["∅"]=> string(7) "∅" ["∇"]=> string(7) "∇" ["∈"]=> string(6) "∈" ["∉"]=> string(7) "∉" ["∋"]=> string(4) "∋" ["∏"]=> string(6) "∏" ["∑"]=> string(5) "∑" ["−"]=> string(7) "−" ["∗"]=> string(8) "∗" ["√"]=> string(7) "√" ["∝"]=> string(6) "∝" ["∞"]=> string(7) "∞" ["∠"]=> string(5) "∠" ["∧"]=> string(5) "∧" ["∨"]=> string(4) "∨" ["∩"]=> string(5) "∩" ["∪"]=> string(5) "∪" ["∫"]=> string(5) "∫" ["∴"]=> string(8) "∴" ["∼"]=> string(5) "∼" ["≅"]=> string(6) "≅" ["≈"]=> string(7) "≈" ["≠"]=> string(4) "≠" ["≡"]=> string(7) "≡" ["≤"]=> string(4) "≤" ["≥"]=> string(4) "≥" ["⊂"]=> string(5) "⊂" ["⊃"]=> string(5) "⊃" ["⊄"]=> string(6) "⊄" ["⊆"]=> string(6) "⊆" ["⊇"]=> string(6) "⊇" ["⊕"]=> string(7) "⊕" ["⊗"]=> string(8) "⊗" ["⊥"]=> string(6) "⊥" ["⋅"]=> string(6) "⋅" ["⌈"]=> string(7) "⌈" ["⌉"]=> string(7) "⌉" ["⌊"]=> string(8) "⌊" ["⌋"]=> string(8) "⌋" ["〈"]=> string(6) "⟨" ["〉"]=> string(6) "⟩" ["◊"]=> string(5) "◊" ["♠"]=> string(8) "♠" ["♣"]=> string(7) "♣" ["♥"]=> string(8) "♥" ["♦"]=> string(7) "♦" ["""]=> string(6) """ ["&"]=> string(5) "&" ["<"]=> string(4) "<" [">"]=> string(4) ">" ["'"]=> string(6) "'" } http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/tests/html_entities2.phpt?view=markup&rev=1.1 Index: pear/HTML_Entities/tests/html_entities2.phpt +++ pear/HTML_Entities/tests/html_entities2.phpt --TEST-- HTML_Entities::encode tests --FILE-- <?php require_once "HTML/Entities.php"; $text = "HTML special chars: <>\"'&. \n"; $text .= "HTML 2.0 chars: ÀÈÌÒÙàèìòù. \n"; $text .= "HTML 3.2 chars: © ¡ £ ×. \n"; $text .= "HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. \n"; $text .= "-----------------------------------------\n"; echo "UTF8:\n"; echo $text; echo "HTML 2.0:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES, HTML_Entities::HTML20); echo "HTML 3.2:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES, HTML_Entities::HTML32); echo "HTML 4.0:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES, HTML_Entities::HTML40); echo "XHTML 1.0:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES, HTML_Entities::XHTML10); echo "XML:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES, HTML_Entities::XML); echo "Ignore special chars:\n"; echo HTML_Entities::encode('<p>'.$text.'</p>', HTML_Entities::NAMES, HTML_Entities::HTML40 | HTML_Entities::IGNORE_SPECIAL_CHARS); echo "Numerical entities:\n"; echo HTML_Entities::encode($text, HTML_Entities::CODES); echo "HTML 2.0 + numerical:\n"; echo HTML_Entities::encode($text, HTML_Entities::NAMES | HTML_Entities::CODES, HTML_Entities::HTML20); ?> --EXPECT-- UTF8: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- HTML 2.0: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- HTML 3.2: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- HTML 4.0: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- XHTML 1.0: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- XML: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- Ignore special chars: <p>HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- </p>Numerical entities: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- HTML 2.0 + numerical: HTML special chars: <>"'&. HTML 2.0 chars: ÀÈÌÒÙàèìòù. HTML 3.2 chars: © ¡ £ ×. HTML 4.0 chars: Œ œ Δ ‡ ♠♣♥♦. ----------------------------------------- http://cvs.php.net/viewvc.cgi/pear/HTML_Entities/tests/html_entities3.phpt?view=markup&rev=1.1 Index: pear/HTML_Entities/tests/html_entities3.phpt +++ pear/HTML_Entities/tests/html_entities3.phpt --TEST-- HTML_Entities::decode tests --FILE-- <?php require_once "HTML/Entities.php"; $text = "HTML 2.0: ÅÆÇÈÉ;Ê\n"; $text .= "HTML 3.2: ¼½¾¿×÷\n"; $text .= "HTML 4.0: ŒœŠšŸƒˆ˜\n"; $text .= "XML: '\n"; $text .= "Numerical: é ! Φ ⊕ 𐀀 -"; echo HTML_Entities::decode($text); ?> --EXPECT-- HTML 2.0: ÅÆÇÈÉ;Ê HTML 3.2: ¼½¾¿×÷ HTML 4.0: ŒœŠšŸƒˆ˜ XML: ' Numerical: é ! Φ ⊕ 𐀀 -