【php开源代码栏目提醒】:网学会员在php开源代码频道为大家收集整理了entity.php提供大家参考,希望对大家有所帮助!
<?php
class entity {
function named_to_numeric ($string) {
$string = preg_replace('/(&[0-9A-Za-z]+)(;?\=?|([^A-Za-z0-9\;\:\.\-\_]))/e', "entity::_named('\\1', '\\2') . '\\3'", $string);
return $string;
}
function normalize_numeric ($string) {
global $_entities;
$string = preg_replace('/&#([0-9]+)(;)?/e', "'&#x'.dechex('\\1').';'", $string);
$string = preg_replace('/&#[Xx](0)*([0-9A-Fa-f]+)(;?|([^A-Za-z0-9\;\:\.\-\_]))/e', "'&#x' . strtoupper('\\2') . ';\\4'", $string);
$string = strtr($string, $_entities['cp1251']);
return $string;
}
function numeric_to_utf8 ($string) {
$string = preg_replace('/&#([0-9]+)(;)?/e', "'&#x'.dechex('\\1').';'", $string);
$string = preg_replace('/&#[Xx](0)*([0-9A-Fa-f]+)(;?|([^A-Za-z0-9\;\:\.\-\_]))/e', "'&#x' . strtoupper('\\2') . ';\\4'", $string);
$string = preg_replace('/&#x([0-9A-Fa-f]+);/e', "entity::_hex_to_utf8('\\1')", $string);
return $string;
}
function numeric_to_named ($string) {
global $_entities;
$string = preg_replace('/&#[Xx]([0-9A-Fa-f]+)/e', "'&#'.hexdec('\\1')", $string);
$string = strtr($string, array_flip($_entities['named']));
return $string;
}
function specialchars ($string, $type = 'xml') {
$apos = $type == '
xml' ? ''' : ''';
$specialchars = array (
'"' => '"', '&' => '&',
''' => $apos, '<' => '<',
'>' => '>', '"' => '"',
'&' => '&', "'" => $apos,
'<' => '<', '>' => '>'
);
$string = preg_replace('/&(#?[Xx]?[0-9A-Za-z]+);/', "[[[ENTITY:\\1]]]", $string);
$string = strtr($string, $specialchars);
$string = preg_replace('/\[\[\[ENTITY\:([^\]]+)\]\]\]/', "&\\1;", $string);
return $string;
}
function _hex_to_utf8($s)
{
$c = hexdec($s);
if ($c < 0x80) {
$str = chr($c);
}
else if ($c < 0x800) {
$str = chr(0xC0 | $c>>6) . chr(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str = chr(0xE0 | $c>>12) . chr(0x80 | $c>>6 & 0x3F) . chr(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str = chr(0xF0 | $c>>18) . chr(0x80 | $c>>12 & 0x3F) . chr(0x80 | $c>>6 & 0x3F) . chr(0x80 | $c & 0x3F);
}
return $str;
}
function _named($entity, $extra) {
global $_entities;
if ($extra == '=') return $entity . '=';
$length = strlen($entity);
while ($length > 0) {
$check = substr($entity, 0, $length);
if (isset($_entities['named'][$check])) return $_entities['named'][$check] . ';' . substr($entity, $length);
$length--;
}
return $entity . ($extra == ';' ? ';' : '');
}
}
$_entities['cp1251'] = array (
'€' => '€', '‚' => '‚', 'ƒ' => 'ƒ',
'„' => '„', '…' => '…', '†' => '†',
'‡' => '‡', 'ˆ' => 'ˆ', '‰' => '‰',
'Š' => 'Š', '‹' => '‹', 'Œ' => 'Œ',
'Ž' => 'Ž', '‘' => '‘', '’' => '’',
'“' => '“', '”' => '”', '•' => '•',
'–' => '–', '—' => '—', '˜' => '˜',
'™' => '™', 'š' => 'š', '›' => '›',
'œ' => 'œ', 'ž' => 'ž', 'Ÿ' => 'Ÿ',
);
$_entities['named'] = array (
' ' => ' ', '¡' => '¡', '¢' => '¢',
'£' => '£', '¤' => '¤', '¥' => '¥',
'¦' => '¦', '§' => '§', '¨' => '¨',
'©' => '©', 'ª' => 'ª', '«' => '«',
'¬' => '¬', '­' => '­', '®' => '®',
'¯' => '¯', '°' => '°', '±' => '±',
'²' => '²', '³' => '³', '´' => '´',
'µ' => 'µ', '¶' => '¶', '&a