首页 » 后端 » PHP » 正文

PHP:将汉字转换为 html 编码的函数,例:瀍 → 瀍

发布者:站点默认
2011/12/16 浏览数(2,105) 分类:PHP PHP:将汉字转换为 html 编码的函数,例:瀍 → 瀍已关闭评论

示例:

“瀍”转换后的编码为“瀍”

用法:

echo unicode_encode('瀍');

说明:

php 6.0及以上版本可以直接使用 unicode_encode 函数,php 6.0以下版本需要自己写这个函数。

代码:

function unicode_encode($str, $encoding='UTF-8', $prefix='&#', $postfix=';'){
	$str = iconv($encoding, 'UCS-2', $str);
	$arrstr = str_split($str, 2);
	$unistr = '';
	for($i=0, $len=count($arrstr); $i<$len; $i++){  
	$dec = hexdec(bin2hex($arrstr[$i]));
	$unistr .= $prefix.$dec.$postfix;
	}
	return $unistr;
}

<完>

点击返回顶部
  1. 留言
  2. 联系方式