src/HtmlEntities.php
<?php
namespace GGG;
class HtmlEntities
{
private $decoded;
private $encoded;
private $decodedChars = array(
'0' => '?',
'1' => '!',
'2' => '@',
'3' => '#',
'4' => '%',
'5' => '$',
'6' => '*',
'7' => '{',
'8' => '}',
'9' => '[',
'10' => ']',
'11' => '(',
'12' => ')',
'13' => '.',
'14' => ',',
'15' => ':',
'16' => ';',
'17' => '+',
'18' => '=',
'19' => '-',
'20' => '_',
'21' => '•',
'22' => '/',
'23' => '\\',
'24' => '\'',
'25' => '"',
'26' => '&',
'27' => '<',
'28' => '>',
'29' => ' ',
'30' => '¡',
'31' => '¢',
'32' => '£',
'33' => '¤',
'34' => '¥',
'35' => '¦',
'36' => '§',
'37' => '¨',
'38' => '©',
'39' => 'ª',
'40' => '«',
'41' => '¬',
'42' => '',
'43' => '®',
'44' => '¯',
'45' => '°',
'46' => '±',
'47' => '²',
'48' => '³',
'49' => '´',
'50' => 'µ',
'51' => '¶',
'52' => '·',
'53' => '¸',
'54' => '¹',
'55' => 'º',
'56' => '»',
'57' => '¼',
'58' => '½',
'59' => '¾',
'60' => '¿',
'61' => 'À',
'62' => 'Á',
'63' => 'Â',
'64' => 'Ã',
'65' => 'Ä',
'66' => 'Å',
'67' => 'Æ',
'68' => 'Ç',
'69' => 'È',
'70' => 'É',
'71' => 'Ê',
'72' => 'Ë',
'73' => 'Ì',
'74' => 'Í',
'75' => 'Î',
'76' => 'Ï',
'77' => 'Ð',
'78' => 'Ñ',
'79' => 'Ò',
'80' => 'Ó',
'81' => 'Ô',
'82' => 'Õ',
'83' => 'Ö',
'84' => '×',
'85' => 'Ø',
'86' => 'Ù',
'87' => 'Ú',
'88' => 'Û',
'89' => 'Ü',
'90' => 'Ý',
'91' => 'Þ',
'92' => 'ß',
'93' => 'à',
'94' => 'á',
'95' => 'â',
'96' => 'ã',
'97' => 'ä',
'98' => 'å',
'99' => 'æ',
'100' => 'ç',
'101' => 'è',
'102' => 'é',
'103' => 'ê',
'104' => 'ë',
'105' => 'ì',
'106' => 'í',
'107' => 'î',
'108' => 'ï',
'109' => 'ð',
'110' => 'ñ',
'111' => 'ò',
'112' => 'ó',
'113' => 'ô',
'114' => 'õ',
'115' => 'ö',
'116' => '÷',
'117' => 'ø',
'118' => 'ù',
'119' => 'ú',
'120' => 'û',
'121' => 'ü',
'122' => 'ý',
'123' => 'þ',
'124' => 'ÿ',
'125' => 'Œ',
'126' => 'œ',
'127' => 'Š',
'128' => 'š',
'129' => 'Ÿ',
'130' => 'ƒ',
'131' => 'ˆ',
'132' => '˜',
'133' => 'Α',
'134' => 'Β',
'135' => 'Γ',
'136' => 'Δ',
'137' => 'Ε',
'138' => 'Ζ',
'139' => 'Η',
'140' => 'Θ',
'141' => 'Ι',
'142' => 'Κ',
'143' => 'Λ',
'144' => 'Μ',
'145' => 'Ν',
'146' => 'Ξ',
'147' => 'Ο',
'148' => 'Π',
'149' => 'Ρ',
'150' => 'Σ',
'151' => 'Τ',
'152' => 'Υ',
'153' => 'Φ',
'154' => 'Χ',
'155' => 'Ψ',
'156' => 'Ω',
'157' => 'α',
'158' => 'β',
'159' => 'γ',
'160' => 'δ',
'161' => 'ε',
'162' => 'ζ',
'163' => 'η',
'164' => 'θ',
'165' => 'ι',
'166' => 'κ',
'167' => 'λ',
'168' => 'μ',
'169' => 'ν',
'170' => 'ξ',
'171' => 'ο',
'172' => 'π',
'173' => 'ρ',
'174' => 'ς',
'175' => 'σ',
'176' => 'τ',
'177' => 'υ',
'178' => 'φ',
'179' => 'χ',
'180' => 'ψ',
'181' => 'ω',
'182' => 'ϑ',
'183' => 'ϒ',
'184' => 'ϖ',
'185' => ' ',
'186' => ' ',
'187' => ' ',
'188' => '',
'189' => '',
'190' => '',
'191' => '',
'192' => '–',
'193' => '—',
'194' => '‘',
'195' => '’',
'196' => '‚',
'197' => '“',
'198' => '”',
'199' => '„',
'200' => '†',
'201' => '‡',
'202' => '•',
'203' => '…',
'204' => '‰',
'205' => '′',
'206' => '″',
'207' => '‹',
'208' => '›',
'209' => '‾',
'210' => '⁄',
'211' => '€',
'212' => 'ℑ',
'213' => '℘',
'214' => 'ℜ',
'215' => '™',
'216' => 'ℵ',
'217' => '←',
'218' => '↑',
'219' => '→',
'220' => '↓',
'221' => '↔',
'222' => '↵',
'223' => '⇐',
'224' => '⇑',
'225' => '⇒',
'226' => '⇓',
'227' => '⇔',
'228' => '∀',
'229' => '∂',
'230' => '∃',
'231' => '∅',
'232' => '∇',
'233' => '∈',
'234' => '∉',
'235' => '∋',
'236' => '∏',
'237' => '∑',
'238' => '−',
'239' => '∗',
'240' => '√',
'241' => '∝',
'242' => '∞',
'243' => '∠',
'244' => '∧',
'245' => '∨',
'246' => '∩',
'247' => '∪',
'248' => '∫',
'249' => '∴',
'250' => '∼',
'251' => '≅',
'252' => '≈',
'253' => '≠',
'254' => '≡',
'255' => '≤',
'256' => '≥',
'257' => '⊂',
'258' => '⊃',
'259' => '⊄',
'260' => '⊆',
'261' => '⊇',
'262' => '⊕',
'263' => '⊗',
'264' => '⊥',
'265' => '⋅',
'266' => '⌈',
'267' => '⌉',
'268' => '⌊',
'269' => '⌋',
'270' => '〈',
'271' => '〉',
'272' => '◊',
'273' => '♠',
'274' => '♣',
'275' => '♥',
'276' => '♦'
);
private $encodedChars = array(
'0' => '&quest;',
'1' => '&excl;',
'2' => '&commat;',
'3' => '&num;',
'4' => '&percnt;',
'5' => '&dollar;',
'6' => '&ast;',
'7' => '&lbrace;',
'8' => '&rbrace;',
'9' => '&lbrack;',
'10' => '&rbrack;',
'11' => '&lpar;',
'12' => '&rpar;',
'13' => '&period;',
'14' => '&comma;',
'15' => '&colon;',
'16' => '&semi;',
'17' => '&plus;',
'18' => '&equals;',
'19' => '&hyphen;',
'20' => '&lowbar;',
'21' => '&middot;',
'22' => '&sol;',
'23' => '&bsol;',
'24' => '&apos;',
'25' => '&quot;',
'26' => '&amp;',
'27' => '&lt;',
'28' => '&gt;',
'29' => '&nbsp;',
'30' => '&iexcl;',
'31' => '&cent;',
'32' => '&pound;',
'33' => '&curren;',
'34' => '&yen;',
'35' => '&brvbar;',
'36' => '&sect;',
'37' => '&uml;',
'38' => '&copy;',
'39' => '&ordf;',
'40' => '&laquo;',
'41' => '&not;',
'42' => '&shy;',
'43' => '&reg;',
'44' => '&macr;',
'45' => '&deg;',
'46' => '&plusmn;',
'47' => '&sup2;',
'48' => '&sup3;',
'49' => '&acute;',
'50' => '&micro;',
'51' => '&para;',
'52' => '&middot;',
'53' => '&cedil;',
'54' => '&sup1;',
'55' => '&ordm;',
'56' => '&raquo;',
'57' => '&frac14;',
'58' => '&frac12;',
'59' => '&frac34;',
'60' => '&iquest;',
'61' => '&Agrave;',
'62' => '&Aacute;',
'63' => '&Acirc;',
'64' => '&Atilde;',
'65' => '&Auml;',
'66' => '&Aring;',
'67' => '&AElig;',
'68' => '&Ccedil;',
'69' => '&Egrave;',
'70' => '&Eacute;',
'71' => '&Ecirc;',
'72' => '&Euml;',
'73' => '&Igrave;',
'74' => '&Iacute;',
'75' => '&Icirc;',
'76' => '&Iuml;',
'77' => '&ETH;',
'78' => '&Ntilde;',
'79' => '&Ograve;',
'80' => '&Oacute;',
'81' => '&Ocirc;',
'82' => '&Otilde;',
'83' => '&Ouml;',
'84' => '&times;',
'85' => '&Oslash;',
'86' => '&Ugrave;',
'87' => '&Uacute;',
'88' => '&Ucirc;',
'89' => '&Uuml;',
'90' => '&Yacute;',
'91' => '&THORN;',
'92' => '&szlig;',
'93' => '&agrave;',
'94' => '&aacute;',
'95' => '&acirc;',
'96' => '&atilde;',
'97' => '&auml;',
'98' => '&aring;',
'99' => '&aelig;',
'100' => '&ccedil;',
'101' => '&egrave;',
'102' => '&eacute;',
'103' => '&ecirc;',
'104' => '&euml;',
'105' => '&igrave;',
'106' => '&iacute;',
'107' => '&icirc;',
'108' => '&iuml;',
'109' => '&eth;',
'110' => '&ntilde;',
'111' => '&ograve;',
'112' => '&oacute;',
'113' => '&ocirc;',
'114' => '&otilde;',
'115' => '&ouml;',
'116' => '&divide;',
'117' => '&oslash;',
'118' => '&ugrave;',
'119' => '&uacute;',
'120' => '&ucirc;',
'121' => '&uuml;',
'122' => '&yacute;',
'123' => '&thorn;',
'124' => '&yuml;',
'125' => '&OElig;',
'126' => '&oelig;',
'127' => '&Scaron;',
'128' => '&scaron;',
'129' => '&Yuml;',
'130' => '&fnof;',
'131' => '&circ;',
'132' => '&tilde;',
'133' => '&Alpha;',
'134' => '&Beta;',
'135' => '&Gamma;',
'136' => '&Delta;',
'137' => '&Epsilon;',
'138' => '&Zeta;',
'139' => '&Eta;',
'140' => '&Theta;',
'141' => '&Iota;',
'142' => '&Kappa;',
'143' => '&Lambda;',
'144' => '&Mu;',
'145' => '&Nu;',
'146' => '&Xi;',
'147' => '&Omicron;',
'148' => '&Pi;',
'149' => '&Rho;',
'150' => '&Sigma;',
'151' => '&Tau;',
'152' => '&Upsilon;',
'153' => '&Phi;',
'154' => '&Chi;',
'155' => '&Psi;',
'156' => '&Omega;',
'157' => '&alpha;',
'158' => '&beta;',
'159' => '&gamma;',
'160' => '&delta;',
'161' => '&epsilon;',
'162' => '&zeta;',
'163' => '&eta;',
'164' => '&theta;',
'165' => '&iota;',
'166' => '&kappa;',
'167' => '&lambda;',
'168' => '&mu;',
'169' => '&nu;',
'170' => '&xi;',
'171' => '&omicron;',
'172' => '&pi;',
'173' => '&rho;',
'174' => '&sigmaf;',
'175' => '&sigma;',
'176' => '&tau;',
'177' => '&upsilon;',
'178' => '&phi;',
'179' => '&chi;',
'180' => '&psi;',
'181' => '&omega;',
'182' => '&thetasym;',
'183' => '&upsih;',
'184' => '&piv;',
'185' => '&ensp;',
'186' => '&emsp;',
'187' => '&thinsp;',
'188' => '&zwnj;',
'189' => '&zwj;',
'190' => '&lrm;',
'191' => '&rlm;',
'192' => '&ndash;',
'193' => '&mdash;',
'194' => '&lsquo;',
'195' => '&rsquo;',
'196' => '&sbquo;',
'197' => '&ldquo;',
'198' => '&rdquo;',
'199' => '&bdquo;',
'200' => '&dagger;',
'201' => '&Dagger;',
'202' => '&bull;',
'203' => '&hellip;',
'204' => '&permil;',
'205' => '&prime;',
'206' => '&Prime;',
'207' => '&lsaquo;',
'208' => '&rsaquo;',
'209' => '&oline;',
'210' => '&frasl;',
'211' => '&euro;',
'212' => '&image;',
'213' => '&weierp;',
'214' => '&real;',
'215' => '&trade;',
'216' => '&alefsym;',
'217' => '&larr;',
'218' => '&uarr;',
'219' => '&rarr;',
'220' => '&darr;',
'221' => '&harr;',
'222' => '&crarr;',
'223' => '&lArr;',
'224' => '&uArr;',
'225' => '&rArr;',
'226' => '&dArr;',
'227' => '&hArr;',
'228' => '&forall;',
'229' => '&part;',
'230' => '&exist;',
'231' => '&empty;',
'232' => '&nabla;',
'233' => '&isin;',
'234' => '&notin;',
'235' => '&ni;',
'236' => '&prod;',
'237' => '&sum;',
'238' => '&minus;',
'239' => '&lowast;',
'240' => '&radic;',
'241' => '&prop;',
'242' => '&infin;',
'243' => '&ang;',
'244' => '&and;',
'245' => '&or;',
'246' => '&cap;',
'247' => '&cup;',
'248' => '&int;',
'249' => '&there4;',
'250' => '&sim;',
'251' => '&cong;',
'252' => '&asymp;',
'253' => '&ne;',
'254' => '&equiv;',
'255' => '&le;',
'256' => '&ge;',
'257' => '&sub;',
'258' => '&sup;',
'259' => '&nsub;',
'260' => '&sube;',
'261' => '&supe;',
'262' => '&oplus;',
'263' => '&otimes;',
'264' => '&perp;',
'265' => '&sdot;',
'266' => '&lceil;',
'267' => '&rceil;',
'268' => '&lfloor;',
'269' => '&rfloor;',
'270' => '&lang;',
'271' => '&rang;',
'272' => '&loz;',
'273' => '&spades;',
'274' => '&clubs;',
'275' => '&hearts;',
'276' => '&diams;'
);
public function __construct()
{
return $this;
}
public function decode($input)
{
$decoded = null;
$key = null;
$encodedChars = $this->encodedChars;
$decodedChars = $this->decodedChars;
if (is_array($input))
{
$decoded = array();
foreach ($input as $index => $char)
{
if (in_array($char, $encodedChars))
{
$keys = array_keys($encodedChars, $char);
$decoded[$index] = $decodedChars[$keys[0]];
}
else
{
$decoded[$index] = $char;
}
}
$decoded = implode('', $decoded);
}
if (is_string($input))
{
if (strlen($input) === 1)
{
if (in_array($input, $encodedChars))
{
$flipped = array_flip($encodedChars);
$key = $flipped[$input];
$decoded = $decodedChars[$key];
}
}
if (strlen($input) >= 2)
{
$decoded = array();
preg_match_all('/((\&\;\w+\;)|([\w\d\s]{1,}))/i', $input, $matches);
$chars = $matches[1];
foreach ($chars as $index => $char)
{
if (in_array($char, $encodedChars))
{
$keys = array_keys($encodedChars, $char);
$decoded[$index] = $decodedChars[$keys[0]];
}
else
{
$decoded[$index] = $char;
}
}
$decoded = implode('', $decoded);
}
$this->decoded = $decoded;
}
return $this->getDecoded();
}
private function getDecoded()
{
return $this->decoded;
}
public function encode($input)
{
$encoded = null;
$key = null;
$decodedChars = $this->decodedChars;
$encodedChars = $this->encodedChars;
if (is_array($input))
{
$encoded = array();
foreach ($input as $index => $char)
{
if (in_array($char, $decodedChars))
{
$keys = array_keys($decodedChars, $char);
$encoded[$index] = $encodedChars[$keys[0]];
}
else
{
$encoded[$index] = $char;
}
}
$encoded = implode('', $encoded);
}
if (is_string($input))
{
if (strlen($input) === 1)
{
if (in_array($input, $decodedChars))
{
$flipped = array_flip($decodedChars);
$key = $flipped[$input];
$encoded = $encodedChars[$key];
}
}
if (strlen($input) >= 2)
{
$encoded = array();
$chars = str_split($input);
foreach ($chars as $index => $char)
{
if (in_array($char, $decodedChars))
{
$keys = array_keys($decodedChars, $char);
$encoded[$index] = $encodedChars[$keys[0]];
}
else
{
$encoded[$index] = $char;
}
}
$encoded = implode('', $encoded);
}
$this->encoded = $encoded;
}
return $this->getEncoded();
}
private function getEncoded()
{
return $this->encoded;
}
}