NCR

人民网

NCR转字符串的方法:

echo mb_convert_encoding ("人民网", "GBK", "HTML-ENTITIES");

纯函数转换的方法:

<?php
$str = '&#20154;&#27665;&#32593;';
echo convert_unicode($str);

function u2utf8($c) {
	$str='';
	if ($c < 0x80) {
		$str.=$c;
	} else if ($c < 0x800) {
		$str.=chr(0xC0 | $c>>6);
		$str.=chr(0x80 | $c & 0x3F);
	} else if ($c < 0x10000) {
		$str.=chr(0xE0 | $c>>12);
		$str.=chr(0x80 | $c>>6 & 0x3F);
			$str.=chr(0x80 | $c & 0x3F);
	} else if ($c < 0x200000) {
		$str.=chr(0xF0 | $c>>18);
		$str.=chr(0x80 | $c>>12 & 0x3F);
		$str.=chr(0x80 | $c>>6 & 0x3F);
		$str.=chr(0x80 | $c & 0x3F);
	}
	return $str;
}

function convert_unicode($source)
{
	preg_match_all("/&#([0-9]+)/",$source, $regs);
	foreach($regs[1] as $v)
	{
		$source = str_replace("&#".$v.";",u2utf8($v),$source);
	}
	return $source;
}

参考资料

php/ncr.txt · 最后更改: 2010/02/26 20:21 由 kenvin
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0 红麦软件 红麦软件