Вход Регистрация
Файл: social12/utf8_html_entity_decode.php
Строк: 374
<?php
/**
 * Convert all HTML entities to UTF-8 characters
 * Функция декодирует гораздо больше именованных сущностей, чем стандартная html_entity_decode()
 * Все dec и hex сущности так же переводятся в UTF-8.
 *
 * @param    string   $s
 * @param    bool     $is_htmlspecialchars   обрабатывать специальные html сущности? (&lt; &gt; &amp; &quot;)
 * @return   string
 * @link     http://www.htmlhelp.com/reference/html40/entities/
 * @link     http://www.alanwood.net/demos/ent4_frame.html (HTML 4.01 Character Entity References)
 * @link     http://msdn.microsoft.com/workshop/author/dhtml/reference/charsets/charset1.asp?frame=true
 * @link     http://msdn.microsoft.com/workshop/author/dhtml/reference/charsets/charset2.asp?frame=true
 * @link     http://msdn.microsoft.com/workshop/author/dhtml/reference/charsets/charset3.asp?frame=true
 *
 * @author   Nasibullin Rinat <n a s i b u l l i n  at starlink ru>
 * @charset  ANSI
 * @version  2.1.12
 */
function utf8_html_entity_decode($s$is_htmlspecialchars false)
{
    
#оптимизация скорости
    
if (strlen($s) < 4  #по минимальной длине сущности - 4 байта: &#d; &xx;
        
|| ($pos strpos($s'&') === false) || strpos($s';'$pos) === false) return $s;
    
$table = array(
      
#Latin-1 Entities:
        
'&nbsp;'   => "xc2xa0",  #no-break space = non-breaking space
        
'&iexcl;'  => "xc2xa1",  #inverted exclamation mark
        
'&cent;'   => "xc2xa2",  #cent sign
        
'&pound;'  => "xc2xa3",  #pound sign
        
'&curren;' => "xc2xa4",  #currency sign
        
'&yen;'    => "xc2xa5",  #yen sign = yuan sign
        
'&brvbar;' => "xc2xa6",  #broken bar = broken vertical bar
        
'&sect;'   => "xc2xa7",  #section sign
        
'&uml;'    => "xc2xa8",  #diaeresis = spacing diaeresis
        
'&copy;'   => "xc2xa9",  #copyright sign
        
'&ordf;'   => "xc2xaa",  #feminine ordinal indicator
        
'&laquo;'  => "xc2xab",  #left-pointing double angle quotation mark = left pointing guillemet («)
        
'&not;'    => "xc2xac",  #not sign
        
'&shy;'    => "xc2xad",  #soft hyphen = discretionary hyphen
        
'&reg;'    => "xc2xae",  #registered sign = registered trade mark sign
        
'&macr;'   => "xc2xaf",  #macron = spacing macron = overline = APL overbar
        
'&deg;'    => "xc2xb0",  #degree sign
        
'&plusmn;' => "xc2xb1",  #plus-minus sign = plus-or-minus sign
        
'&sup2;'   => "xc2xb2",  #superscript two = superscript digit two = squared
        
'&sup3;'   => "xc2xb3",  #superscript three = superscript digit three = cubed
        
'&acute;'  => "xc2xb4",  #acute accent = spacing acute
        
'&micro;'  => "xc2xb5",  #micro sign
        
'&para;'   => "xc2xb6",  #pilcrow sign = paragraph sign
        
'&middot;' => "xc2xb7",  #middle dot = Georgian comma = Greek middle dot
        
'&cedil;'  => "xc2xb8",  #cedilla = spacing cedilla
        
'&sup1;'   => "xc2xb9",  #superscript one = superscript digit one
        
'&ordm;'   => "xc2xba",  #masculine ordinal indicator
        
'&raquo;'  => "xc2xbb",  #right-pointing double angle quotation mark = right pointing guillemet (»)
        
'&frac14;' => "xc2xbc",  #vulgar fraction one quarter = fraction one quarter
        
'&frac12;' => "xc2xbd",  #vulgar fraction one half = fraction one half
        
'&frac34;' => "xc2xbe",  #vulgar fraction three quarters = fraction three quarters
        
'&iquest;' => "xc2xbf",  #inverted question mark = turned question mark
      #Latin capital letter
        
'&Agrave;' => "xc3x80",  #Latin capital letter A with grave = Latin capital letter A grave
        
'&Aacute;' => "xc3x81",  #Latin capital letter A with acute
        
'&Acirc;'  => "xc3x82",  #Latin capital letter A with circumflex
        
'&Atilde;' => "xc3x83",  #Latin capital letter A with tilde
        
'&Auml;'   => "xc3x84",  #Latin capital letter A with diaeresis
        
'&Aring;'  => "xc3x85",  #Latin capital letter A with ring above = Latin capital letter A ring
        
'&AElig;'  => "xc3x86",  #Latin capital letter AE = Latin capital ligature AE
        
'&Ccedil;' => "xc3x87",  #Latin capital letter C with cedilla
        
'&Egrave;' => "xc3x88",  #Latin capital letter E with grave
        
'&Eacute;' => "xc3x89",  #Latin capital letter E with acute
        
'&Ecirc;'  => "xc3x8a",  #Latin capital letter E with circumflex
        
'&Euml;'   => "xc3x8b",  #Latin capital letter E with diaeresis
        
'&Igrave;' => "xc3x8c",  #Latin capital letter I with grave
        
'&Iacute;' => "xc3x8d",  #Latin capital letter I with acute
        
'&Icirc;'  => "xc3x8e",  #Latin capital letter I with circumflex
        
'&Iuml;'   => "xc3x8f",  #Latin capital letter I with diaeresis
        
'&ETH;'    => "xc3x90",  #Latin capital letter ETH
        
'&Ntilde;' => "xc3x91",  #Latin capital letter N with tilde
        
'&Ograve;' => "xc3x92",  #Latin capital letter O with grave
        
'&Oacute;' => "xc3x93",  #Latin capital letter O with acute
        
'&Ocirc;'  => "xc3x94",  #Latin capital letter O with circumflex
        
'&Otilde;' => "xc3x95",  #Latin capital letter O with tilde
        
'&Ouml;'   => "xc3x96",  #Latin capital letter O with diaeresis
        
'&times;'  => "xc3x97",  #multiplication sign
        
'&Oslash;' => "xc3x98",  #Latin capital letter O with stroke = Latin capital letter O slash
        
'&Ugrave;' => "xc3x99",  #Latin capital letter U with grave
        
'&Uacute;' => "xc3x9a",  #Latin capital letter U with acute
        
'&Ucirc;'  => "xc3x9b",  #Latin capital letter U with circumflex
        
'&Uuml;'   => "xc3x9c",  #Latin capital letter U with diaeresis
        
'&Yacute;' => "xc3x9d",  #Latin capital letter Y with acute
        
'&THORN;'  => "xc3x9e",  #Latin capital letter THORN
      #Latin small letter
        
'&szlig;'  => "xc3x9f",  #Latin small letter sharp s = ess-zed
        
'&agrave;' => "xc3xa0",  #Latin small letter a with grave = Latin small letter a grave
        
'&aacute;' => "xc3xa1",  #Latin small letter a with acute
        
'&acirc;'  => "xc3xa2",  #Latin small letter a with circumflex
        
'&atilde;' => "xc3xa3",  #Latin small letter a with tilde
        
'&auml;'   => "xc3xa4",  #Latin small letter a with diaeresis
        
'&aring;'  => "xc3xa5",  #Latin small letter a with ring above = Latin small letter a ring
        
'&aelig;'  => "xc3xa6",  #Latin small letter ae = Latin small ligature ae
        
'&ccedil;' => "xc3xa7",  #Latin small letter c with cedilla
        
'&egrave;' => "xc3xa8",  #Latin small letter e with grave
        
'&eacute;' => "xc3xa9",  #Latin small letter e with acute
        
'&ecirc;'  => "xc3xaa",  #Latin small letter e with circumflex
        
'&euml;'   => "xc3xab",  #Latin small letter e with diaeresis
        
'&igrave;' => "xc3xac",  #Latin small letter i with grave
        
'&iacute;' => "xc3xad",  #Latin small letter i with acute
        
'&icirc;'  => "xc3xae",  #Latin small letter i with circumflex
        
'&iuml;'   => "xc3xaf",  #Latin small letter i with diaeresis
        
'&eth;'    => "xc3xb0",  #Latin small letter eth
        
'&ntilde;' => "xc3xb1",  #Latin small letter n with tilde
        
'&ograve;' => "xc3xb2",  #Latin small letter o with grave
        
'&oacute;' => "xc3xb3",  #Latin small letter o with acute
        
'&ocirc;'  => "xc3xb4",  #Latin small letter o with circumflex
        
'&otilde;' => "xc3xb5",  #Latin small letter o with tilde
        
'&ouml;'   => "xc3xb6",  #Latin small letter o with diaeresis
        
'&divide;' => "xc3xb7",  #division sign
        
'&oslash;' => "xc3xb8",  #Latin small letter o with stroke = Latin small letter o slash
        
'&ugrave;' => "xc3xb9",  #Latin small letter u with grave
        
'&uacute;' => "xc3xba",  #Latin small letter u with acute
        
'&ucirc;'  => "xc3xbb",  #Latin small letter u with circumflex
        
'&uuml;'   => "xc3xbc",  #Latin small letter u with diaeresis
        
'&yacute;' => "xc3xbd",  #Latin small letter y with acute
        
'&thorn;'  => "xc3xbe",  #Latin small letter thorn
        
'&yuml;'   => "xc3xbf",  #Latin small letter y with diaeresis
      #Symbols and Greek Letters:
        
'&fnof;'    => "xc6x92",  #Latin small f with hook = function = florin
        
'&Alpha;'   => "xcex91",  #Greek capital letter alpha
        
'&Beta;'    => "xcex92",  #Greek capital letter beta
        
'&Gamma;'   => "xcex93",  #Greek capital letter gamma
        
'&Delta;'   => "xcex94",  #Greek capital letter delta
        
'&Epsilon;' => "xcex95",  #Greek capital letter epsilon
        
'&Zeta;'    => "xcex96",  #Greek capital letter zeta
        
'&Eta;'     => "xcex97",  #Greek capital letter eta
        
'&Theta;'   => "xcex98",  #Greek capital letter theta
        
'&Iota;'    => "xcex99",  #Greek capital letter iota
        
'&Kappa;'   => "xcex9a",  #Greek capital letter kappa
        
'&Lambda;'  => "xcex9b",  #Greek capital letter lambda
        
'&Mu;'      => "xcex9c",  #Greek capital letter mu
        
'&Nu;'      => "xcex9d",  #Greek capital letter nu
        
'&Xi;'      => "xcex9e",  #Greek capital letter xi
        
'&Omicron;' => "xcex9f",  #Greek capital letter omicron
        
'&Pi;'      => "xcexa0",  #Greek capital letter pi
        
'&Rho;'     => "xcexa1",  #Greek capital letter rho
        
'&Sigma;'   => "xcexa3",  #Greek capital letter sigma
        
'&Tau;'     => "xcexa4",  #Greek capital letter tau
        
'&Upsilon;' => "xcexa5",  #Greek capital letter upsilon
        
'&Phi;'     => "xcexa6",  #Greek capital letter phi
        
'&Chi;'     => "xcexa7",  #Greek capital letter chi
        
'&Psi;'     => "xcexa8",  #Greek capital letter psi
        
'&Omega;'   => "xcexa9",  #Greek capital letter omega
        
'&alpha;'   => "xcexb1",  #Greek small letter alpha
        
'&beta;'    => "xcexb2",  #Greek small letter beta
        
'&gamma;'   => "xcexb3",  #Greek small letter gamma
        
'&delta;'   => "xcexb4",  #Greek small letter delta
        
'&epsilon;' => "xcexb5",  #Greek small letter epsilon
        
'&zeta;'    => "xcexb6",  #Greek small letter zeta
        
'&eta;'     => "xcexb7",  #Greek small letter eta
        
'&theta;'   => "xcexb8",  #Greek small letter theta
        
'&iota;'    => "xcexb9",  #Greek small letter iota
        
'&kappa;'   => "xcexba",  #Greek small letter kappa
        
'&lambda;'  => "xcexbb",  #Greek small letter lambda
        
'&mu;'      => "xcexbc",  #Greek small letter mu
        
'&nu;'      => "xcexbd",  #Greek small letter nu
        
'&xi;'      => "xcexbe",  #Greek small letter xi
        
'&omicron;' => "xcexbf",  #Greek small letter omicron
        
'&pi;'      => "xcfx80",  #Greek small letter pi
        
'&rho;'     => "xcfx81",  #Greek small letter rho
        
'&sigmaf;'  => "xcfx82",  #Greek small letter final sigma
        
'&sigma;'   => "xcfx83",  #Greek small letter sigma
        
'&tau;'     => "xcfx84",  #Greek small letter tau
        
'&upsilon;' => "xcfx85",  #Greek small letter upsilon
        
'&phi;'     => "xcfx86",  #Greek small letter phi
        
'&chi;'     => "xcfx87",  #Greek small letter chi
        
'&psi;'     => "xcfx88",  #Greek small letter psi
        
'&omega;'   => "xcfx89",  #Greek small letter omega
        
'&thetasym;'=> "xcfx91",  #Greek small letter theta symbol
        
'&upsih;'   => "xcfx92",  #Greek upsilon with hook symbol
        
'&piv;'     => "xcfx96",  #Greek pi symbol

        
'&bull;'    => "xe2x80xa2",  #bullet = black small circle
        
'&hellip;'  => "xe2x80xa6",  #horizontal ellipsis = three dot leader
        
'&prime;'   => "xe2x80xb2",  #prime = minutes = feet (для обозначения минут и футов)
        
'&Prime;'   => "xe2x80xb3",  #double prime = seconds = inches (для обозначения секунд и дюймов).
        
'&oline;'   => "xe2x80xbe",  #overline = spacing overscore
        
'&frasl;'   => "xe2x81x84",  #fraction slash
        
'&weierp;'  => "xe2x84x98",  #script capital P = power set = Weierstrass p
        
'&image;'   => "xe2x84x91",  #blackletter capital I = imaginary part
        
'&real;'    => "xe2x84x9c",  #blackletter capital R = real part symbol
        
'&trade;'   => "xe2x84xa2",  #trade mark sign
        
'&alefsym;' => "xe2x84xb5",  #alef symbol = first transfinite cardinal
        
'&larr;'    => "xe2x86x90",  #leftwards arrow
        
'&uarr;'    => "xe2x86x91",  #upwards arrow
        
'&rarr;'    => "xe2x86x92",  #rightwards arrow
        
'&darr;'    => "xe2x86x93",  #downwards arrow
        
'&harr;'    => "xe2x86x94",  #left right arrow
        
'&crarr;'   => "xe2x86xb5",  #downwards arrow with corner leftwards = carriage return
        
'&lArr;'    => "xe2x87x90",  #leftwards double arrow
        
'&uArr;'    => "xe2x87x91",  #upwards double arrow
        
'&rArr;'    => "xe2x87x92",  #rightwards double arrow
        
'&dArr;'    => "xe2x87x93",  #downwards double arrow
        
'&hArr;'    => "xe2x87x94",  #left right double arrow
        
'&forall;'  => "xe2x88x80",  #for all
        
'&part;'    => "xe2x88x82",  #partial differential
        
'&exist;'   => "xe2x88x83",  #there exists
        
'&empty;'   => "xe2x88x85",  #empty set = null set = diameter
        
'&nabla;'   => "xe2x88x87",  #nabla = backward difference
        
'&isin;'    => "xe2x88x88",  #element of
        
'&notin;'   => "xe2x88x89",  #not an element of
        
'&ni;'      => "xe2x88x8b",  #contains as member
        
'&prod;'    => "xe2x88x8f",  #n-ary product = product sign
        
'&sum;'     => "xe2x88x91",  #n-ary sumation
        
'&minus;'   => "xe2x88x92",  #minus sign
        
'&lowast;'  => "xe2x88x97",  #asterisk operator
        
'&radic;'   => "xe2x88x9a",  #square root = radical sign
        
'&prop;'    => "xe2x88x9d",  #proportional to
        
'&infin;'   => "xe2x88x9e",  #infinity
        
'&ang;'     => "xe2x88xa0",  #angle
        
'&and;'     => "xe2x88xa7",  #logical and = wedge
        
'&or;'      => "xe2x88xa8",  #logical or = vee
        
'&cap;'     => "xe2x88xa9",  #intersection = cap
        
'&cup;'     => "xe2x88xaa",  #union = cup
        
'&int;'     => "xe2x88xab",  #integral
        
'&there4;'  => "xe2x88xb4",  #therefore
        
'&sim;'     => "xe2x88xbc",  #tilde operator = varies with = similar to
        
'&cong;'    => "xe2x89x85",  #approximately equal to
        
'&asymp;'   => "xe2x89x88",  #almost equal to = asymptotic to
        
'&ne;'      => "xe2x89xa0",  #not equal to
        
'&equiv;'   => "xe2x89xa1",  #identical to
        
'&le;'      => "xe2x89xa4",  #less-than or equal to
        
'&ge;'      => "xe2x89xa5",  #greater-than or equal to
        
'&sub;'     => "xe2x8ax82",  #subset of
        
'&sup;'     => "xe2x8ax83",  #superset of
        
'&nsub;'    => "xe2x8ax84",  #not a subset of
        
'&sube;'    => "xe2x8ax86",  #subset of or equal to
        
'&supe;'    => "xe2x8ax87",  #superset of or equal to
        
'&oplus;'   => "xe2x8ax95",  #circled plus = direct sum
        
'&otimes;'  => "xe2x8ax97",  #circled times = vector product
        
'&perp;'    => "xe2x8axa5",  #up tack = orthogonal to = perpendicular
        
'&sdot;'    => "xe2x8bx85",  #dot operator
        
'&lceil;'   => "xe2x8cx88",  #left ceiling = APL upstile
        
'&rceil;'   => "xe2x8cx89",  #right ceiling
        
'&lfloor;'  => "xe2x8cx8a",  #left floor = APL downstile
        
'&rfloor;'  => "xe2x8cx8b",  #right floor
        
'&lang;'    => "xe2x8cxa9",  #left-pointing angle bracket = bra
        
'&rang;'    => "xe2x8cxaa",  #right-pointing angle bracket = ket
        
'&loz;'     => "xe2x97x8a",  #lozenge
        
'&spades;'  => "xe2x99xa0",  #black spade suit
        
'&clubs;'   => "xe2x99xa3",  #black club suit = shamrock
        
'&hearts;'  => "xe2x99xa5",  #black heart suit = valentine
        
'&diams;'   => "xe2x99xa6",  #black diamond suit
      #Other Special Characters:
        
'&OElig;'  => "xc5x92",  #Latin capital ligature OE
        
'&oelig;'  => "xc5x93",  #Latin small ligature oe
        
'&Scaron;' => "xc5xa0",  #Latin capital letter S with caron
        
'&scaron;' => "xc5xa1",  #Latin small letter s with caron
        
'&Yuml;'   => "xc5xb8",  #Latin capital letter Y with diaeresis
        
'&circ;'   => "xcbx86",  #modifier letter circumflex accent
        
'&tilde;'  => "xcbx9c",  #small tilde
        
'&ensp;'   => "xe2x80x82",  #en space
        
'&emsp;'   => "xe2x80x83",  #em space
        
'&thinsp;' => "xe2x80x89",  #thin space
        
'&zwnj;'   => "xe2x80x8c",  #zero width non-joiner
        
'&zwj;'    => "xe2x80x8d",  #zero width joiner
        
'&lrm;'    => "xe2x80x8e",  #left-to-right mark
        
'&rlm;'    => "xe2x80x8f",  #right-to-left mark
        
'&ndash;'  => "xe2x80x93",  #en dash
        
'&mdash;'  => "xe2x80x94",  #em dash
        
'&lsquo;'  => "xe2x80x98",  #left single quotation mark
        
'&rsquo;'  => "xe2x80x99",  #right single quotation mark (and apostrophe!)
        
'&sbquo;'  => "xe2x80x9a",  #single low-9 quotation mark
        
'&ldquo;'  => "xe2x80x9c",  #left double quotation mark
        
'&rdquo;'  => "xe2x80x9d",  #right double quotation mark
        
'&bdquo;'  => "xe2x80x9e",  #double low-9 quotation mark
        
'&dagger;' => "xe2x80xa0",  #dagger
        
'&Dagger;' => "xe2x80xa1",  #double dagger
        
'&permil;' => "xe2x80xb0",  #per mille sign
        
'&lsaquo;' => "xe2x80xb9",  #single left-pointing angle quotation mark
        
'&rsaquo;' => "xe2x80xba",  #single right-pointing angle quotation mark
        
'&euro;'   => "xe2x82xac",  #euro sign
    
);
    
$htmlspecialchars = array(
        
'&quot;' => "x22",  #quotation mark = APL quote (") &#34;
        
'&amp;'  => "x26",  #ampersand                  (&) &#38;
        
'&lt;'   => "x3c",  #less-than sign             (<) &#60;
        
'&gt;'   => "x3e",  #greater-than sign          (>) &#62;
    
);

    if (
$is_htmlspecialchars$table += $htmlspecialchars;

    
#заменяем именованные сущности:
    #оптимизация скорости: заменяем только те сущности, которые используются в html коде!
    #эта часть кода работает быстрее, чем $s = strtr($s, $table);
    
preg_match_all('/&[a-zA-Z]+d*;/s'$s$mnull$pos);
    foreach (
array_unique($m[0]) as $entity)
    {
        if (
array_key_exists($entity$table)) $s str_replace($entity$table[$entity], $s);
    }
#foreach

    
if (($pos strpos($s'&#')) !== false)  #speed optimization
    
{
        
#заменяем числовые dec и hex сущности:
        
$htmlspecialchars_flip array_flip($htmlspecialchars);
        
$s preg_replace(
            
'/&#((x)[da-fA-F]{2,4}|d{1,4});/se',
            
'(array_key_exists($a = pack("C", $d = ("$2") ? hexdec("$1") : "$1"), $htmlspecialchars_flip) && ! $is_htmlspecialchars) ?
             $htmlspecialchars_flip[$a] :
             iconv("UCS-2BE", "UTF-8", pack("n", $d))'
$s, -1$pos);
    }
    return 
$s;
}

?>
Онлайн: 2
Реклама