加入收藏 | 设为首页 | 会员中心 | 我要投稿 天瑞地安资讯网 (https://www.52baoding.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

linux 命令 unicode,linux 下的 UNICODE 编码

发布时间:2022-12-05 14:31:31 所属栏目:Linux 来源:网络
导读: /**
* 将内容进行UNICODE编码,如中文字符串"图片" 转为"\u56fe\u7247"
* @param string $name 要转换的中文字符串
* @param string $in_charset 输入中文编码,默认为uft8
* @param strin

/**

* 将内容进行UNICODE编码,如中文字符串"图片" 转为"\u56fe\u7247"

* @param string $name 要转换的中文字符串

* @param string $in_charset 输入中文编码,默认为uft8

* @param string $out_charset 输出unicode编码,'UCS-2BE'或'UCS-2LE'

* Linux 服务器上 UCS-2 编码方式与 Winodws 不一致,linux编码为UCS-2BE,windows为UCS-2LE,即big-endian和little-endian

* @return string

*/

if (!function_exists('unicode_encode')) {

function unicode_encode($name,$in_charset='UTF-8',$out_charset='UCS-2BE')

{

$name = iconv($in_charset, $out_charset, $name);

$len = strlen($name);

$str = '';

for ($i = 0; $i < $len - 1; $i = $i + 2){

$c = $name[$i];

$c2 = $name[$i + 1];

if (ord($c) > 0){ // 两个字节的文字

$str .= '\u'.base_convert(ord($c), 10, 16).base_convert(ord($c2), 10, 16);

}

else{

$str .= $c2;

}

}

return $str;

}

}

/**

* 将UNICODE编码后的内容进行解码,如unicode字符串:"\u56fe\u7247" 转为"图片"

* @param string $name 要转换的unicode字符串

* @param string $out_charset 输出编码,默认为utf8

* @param string $in_charset 输入unicode编码,Linux 服务器上 UCS-2 编码方式与 Winodws 不一致,

* linux编码为UCS-2BE,windows为UCS-2LElinux编码,即big-endian和little-endian

* @return string

*/

if (!function_exists('unicode_decode')) {

function unicode_decode($name,$out_charset='UTF-8',$in_charset='UCS-2BE')

{

// 转换编码,将Unicode编码转换成可以浏览的utf-8编码

$pattern = '/([\w]+)|(\\\u([\w]{4}))/i';

preg_match_all($pattern, $name, $matches);

if (!empty($matches)){

$name = '';

for ($j = 0; $j < count($matches[0]); $j++){

$str = $matches[0][$j];

if (strpos($str, '\\u') === 0){

$code = base_convert(substr($str, 2, 2), 16, 10);

$code2 = base_convert(substr($str, 4), 16, 10);

$c = chr($code).chr($code2);

$c = iconv($in_charset, $out_charset, $c);

$name .= $c;

}

else{

$name .= $str;

}

}

}

return $name;

}

}

发表评论您的电子邮箱地址不会被公开。 必填项已用*标注

*

*

(编辑:天瑞地安资讯网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!