<?php

// v 2.0

$GLOBALS['_HTMLEntitiesDecode_encoding'] = 'KOI8-R';
$GLOBALS['_HTMLEntitiesDecode_replacements'] =
    array(
	160 => '&nbsp;',
	171 => '&quot;',
	187 => '&quot;',
	8220 => '&quot;',
	8221 => '&quot;',
	8211 => '-',
	8212 => '-',
	9552 => '-'
    );

function _HTMLEntitiesDecode_iconv($num)
{
    $encoding =  $GLOBALS['_HTMLEntitiesDecode_encoding'];

    // unicode number to utf-8
    if ($num < 128)
	$str = chr($num);
    else if ($num < 2048)
	$str = chr(192 + (($num - ($num % 64)) / 64)) . chr(128 + ($num % 64));
    else
	$str = chr(224 + (($num - ($num % 4096)) / 4096)) .
	    chr(128 + ((($num % 4096) - ($num % 64)) / 64)) .
	    chr(128 + ($num % 64));

    $tmp = @iconv("UTF-8", "$encoding//IGNORE", $str);
    if (strlen($tmp) != 0)
	return str_replace(
	    array('"', "'", "<", ">", "&"),
	    array("&quot;", "&apos;", "&lt;", "&gt;", "&amp;"),
	    $tmp);

    if (isset($GLOBALS['_HTMLEntitiesDecode_replacements'][$num]))
	return $GLOBALS['_HTMLEntitiesDecode_replacements'][$num];

    return "&#$num;";
}

function _HTMLEntitiesDecode_cb($matches)
{
    $str = $matches[1];

    if ($str{0} == 'x')
	$str = hexdec(substr($str, 1));
    else
    {
	$str = 0 + preg_replace('/^0+(?=0)/', '', $str);
//	$str = 0 + (strlen($str) == 0 ? 0 : $str);
    }

    return _HTMLEntitiesDecode_iconv($str);
}

function HTMLEntitiesDecode($str, $encoding = '')
{
    if ($encoding == '')
    {
	if (isset($_SERVER['HTTP_ACCEPT_CHARSET']))
	{
	    preg_match('/^\s*([-\w]+?)([,;\s]|$)/', $_SERVER['HTTP_ACCEPT_CHARSET'], $a);
	    $encoding = strtoupper($a[1]);
	}
	else
	{
	    if ($GLOBALS['_HTMLEntitiesDecode_encoding'] == '')
		$encoding = 'KOI8-R'; // default
	}
    }
    $GLOBALS['_HTMLEntitiesDecode_encoding'] = $encoding;

    return preg_replace_callback('/&#(x[[:xdigit:]]{4}|\d{1,5});/', "_HTMLEntitiesDecode_cb", $str);
}

?>