php汉字转Unicode编码函数

/**
 * $str 原始字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码后的前缀,默认"&#"
 * $postfix 编码后的后缀,默认";"
 */
function unicode_encode($str, $encoding = ‘GBK‘, $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;
}
?
/**
 * $str Unicode编码后的字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码字符串的前缀,默认"&#"
 * $postfix 编码字符串的后缀,默认";"
 */
function unicode_decode($unistr, $encoding = ‘GBK‘, $prefix = ‘&#‘, $postfix = ‘;‘) {
    $arruni = explode($prefix, $unistr);
    $unistr = ‘‘;
    for($i = 1, $len = count($arruni); $i < $len; $i++) {
        if (strlen($postfix) > 0) {
            $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
        }
        $temp = intval($arruni[$i]);
        $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
    }
    return iconv(‘UCS-2‘, $encoding, $unistr);
}

使用:
//GBK字符串测试
$str = ‘<b>哈哈</b>‘;
echo $str.‘<br />‘;
?
$unistr = unicode_encode($str);
echo $unistr.‘<br />‘; // <b>哈哈</b>
?
$str2 = unicode_decode($unistr);
echo $str2.‘<br />‘; //<b>哈哈</b>
?
//UTF-8字符串测试
$utf8_str = iconv(‘GBK‘, ‘UTF-8‘, $str);
echo $utf8_str.‘<br />‘; // <b>鍝堝搱</b> 注:UTF在GBK下显示的乱码!可切换浏览器的编码测试
?
$utf8_unistr = unicode_encode($utf8_str, ‘UTF-8‘);
echo $utf8_unistr.‘<br />‘; // <b>哈哈</b>
?
$utf8_str2 = unicode_decode($utf8_unistr, ‘UTF-8‘);
echo $utf8_str2.‘<br />‘; // <b>鍝堝搱</b>
?
//其它后缀、前缀测试
$prefix_unistr = unicode_encode($str, ‘GBK‘, "\\u", ‘‘);
echo $prefix_unistr.‘<br />‘; // \u60\u98\u62\u21704\u21704\u60\u47\u98\u62
?
$profix_unistr2 = unicode_decode($prefix_unistr, ‘GBK‘, "\\u", ‘‘);
echo $profix_unistr2.‘<br />‘; //<b>哈哈</b>

php汉字转Unicode编码函数

时间: 2024-07-30 02:47:45

php汉字转Unicode编码函数的相关文章

winform学习日志(二十八)----------将汉字转化为拼音,正则表达式和得到汉字的Unicode编码

一:上图,不清楚的看代码注解,很详细了 二:具体代码 窗体代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressio

PHP 汉字 特殊字符 UNICODE 编码 解码 高性能简洁实现方案

网上,针对汉字.特殊字符的UNICODE 编码.解码实现,方法诸多,但是大多是复制粘贴,没有新意! PHP UNICODE 汉字 编码: var_dump(json_encode('2018 ABC 我是中国人!网站:http://my.oschina.net/cart/')); 上述就实现了PHP中对汉字.特殊字符的 UNICODE 编码! 多么easy! 上面会输出: string(96) ""2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff0

汉字转为Unicode编码

protected string GetUnicode(string text) { string result = ""; for (int i = 0; i < text.Length; i++) { if ((int)text[i] > 32 && (int)text[i] < 127) { result += text[i].ToString(); } else result += string.Format("\\u{0:x4}&q

Js汉字和Unicode编码互转 Unicode加密 Unicode解密

<title>汉字和Unicode编码互转</title> <script Language=Javascript> var classObj= { ToUnicode:function(str) { return escape(str).replace(/%/g,"\\").toLowerCase(); }, UnUnicode:function(str) { return unescape(str.replace(/\\/g, "%&q

.Net(c#)汉字和Unicode编码互相转换

{"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.cnblogs.com"} 经常遇到这样内容的json字符串,原来是把其中的汉字做了Unicode编码转换. Unicode编码: 将汉字进行UNICODE编码,如:“王”编码后就成了“\王”,UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的256以内的一个数字.而一个汉字是由两

c# 实现获取汉字十六进制Unicode编码字符串

1.  汉字转十六进制UNICODE编码字符串 /// <summary>        /// ////        /// </summary>        /// <param name="character"></param>        /// <returns></returns>        public string CharacterToCoding(string character)  

常用汉字的unicode编码

\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97 \u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u5

将字符串中的(汉字Unicode编码)解析成汉字

前提:服务端使用的是.Net做的网站,安卓端则是使用Java语言开发的,再次期间使用的传输数据格式是Json格式 平时在做项目的时候都是使用的java语言开发的服务端,所以在传输数据的时候虽然也是使用Json格式,但是数据里面并没有进行Unicode编码,在这次做项目的时候,由于是使用C#语言开发的,而且传递的数据里面含有汉字的Unicode编码,这时就需要在客户端对此Unicode编码进行解码,我找了一下字符串的提供的方法,并没有找到直接对汉字进行解码的方法,于是就查找资料自己写了一个解码的工

将Unicode编码转换为汉字字符串

/// <summary> /// 汉字转换为Unicode编码 /// </summary> /// <param name="str">要编码的汉字字符串</param> /// <returns>Unicode编码的的字符串</returns> public static string ToUnicode(string str) { byte[] bts = Encoding.Unicode.GetBytes