中英文混和的字符串的ASCII与UNICODE编码与解码

所属栏目:建站技术 更新时间:2020-10-30 点击: 文章来源:https://www.sznest.net/jzjs/3546.html

//1:获取中英文混合字符的编码(ASCII码与UNICODE码)
$str = "官网制作:网巢网络";
foreach (math($str) as $key => $value) {
    if (preg_match('/^[x{4e00}-x{9fa5}]+$/u',$value)) {//中文,注意中文这里暂时不转换,因为无法通过chr函数来获取中文,
        $arr_chr[]=unicode_encode($value);
    } else {//英文
        $arr_chr[]=asc($value);
    }
}
//再将数组转换成字符串
$strs = implode(",", $arr_chr);


//2:将上面的字符串解析成原来的字符串
$arr_chr = explode(",", $strs);
foreach ($arr_chr as $key => $value) {
    if(is_numeric($value))//如果是数字将ASCII码转换成英文字符
    {
        echo chr($value);
    }else{//否则将UNICODE编码后的内容进行解码成汉字
        echo unicode_decode($value);
    }
}

//函数
//php将字符串分割成数组实现中文分词
function math($string,$code ='UTF-8'){
    if ($code == 'UTF-8') {
     $pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
    } else {
        $pa = "/[x01-x7f]|[xa1-xff][xa1-xff]/";
    }
    preg_match_all($pa, $string, $t_string);
    $math="";
    foreach($t_string[0] as $k=>$s){
        $math[]=$s;
    }
     return $math;
}
//转换成ascii码,注意其实中文是没有ascii码的,所以最好将中文转成UNICODE编码
function asc($s) {
  if(ord($s) < 128) return ord($s);
  //return current(unpack('N', "xffxff$s"));
}
//将中文进行UNICODE编码,编码后的内容格式:u56feu7247
function unicode_encode($name)  
{  
    $name = iconv('UTF-8', 'UCS-2', $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编码后的内容进行解码变成汉字
function unicode_decode($name)  
{  
    // 转换编码,将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('UCS-2', 'UTF-8', $c);  
                $name .= $c;  
            }  
            else  
            {  
                $name .= $str;  
            }  
        }  
    }  
    return $name;  
}

相关阅读

不带www跳转到www,http跳转到https

一:windows操作系统,在网站根目录下,新建web.config这个文件1:不带www跳转到www代码示例&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;UTF-8&quot;?&gt;&lt;configuration&gt;&lt;system.webServer&gt;&lt;rewrite&gt;&lt…

了解详情 >>

常用批量替换的正则表达式-批量删除tppabs

匹配一行的开头,然后在一行开头前加入指定字符串,比如修改hosts文件在域名前面加入127.0.0.1^(.{1,})127.0.0.1$1如果是sublimeText编辑器,按ctrl+H,然后在弹出的窗口中点击“.*”,如果要匹配一行的行尾,则是^(.{1,})$匹配A标签的超连接的正则表式如下$pattern=&quot;/&lt;a[^&gt;]+?…

了解详情 >>

SublimeText2.0.2完整的注册码及安装插件说明及…

注册码在help的EnterLicense中输入下面所有的字符,无须去空格和换行,所有的字符就是完整的注册码-----BEGINLICENSE-----AndrewWeberSingleUserLicenseEA7E-855605813A03DD5E4AD9E66C0EEB94BC99798F942194A602396E98E62C99794BB979FE9…

了解详情 >>

如何将程序添加到右键菜单中?

方法一:在HKEY_CLASSES_ROOT*shell下面添加sublime_textCommand,修改Command项的默认值的数据数值为&quot;D:我的文档sublimetext3sublime_text.exe&quot;&quot;%1&quot;改了之后此方法你会发现不管选择什么类型的文件都会出现在右键菜单中方法二:修改HKEY_CLASS…

了解详情 >>

360浏览器header的Location跳转无效的解决办法

在百度里搜索这个问题的答案,有的说是BOM头导致的问题,有的点是因为之前有输出语句导致的问题,有的话是Location和后面的:(冒号)之间有空格导致的问题,还有的话是因为没有加上exit()导致的问题,但是这几项都排除之外,用360浏览器8.1版本的仍然会有这种情况的发生,那么可以接着继续往下看如果用360浏览器8.1版本的打开“你的域名/admin/te…

了解详情 >>

TP5请求信息与请求参数

&lt;?phpnamespaceappindexcontroller;usethinkController;usethinkRequest;classIndexextendsController{publicfunctionindex(){//index/index/index/name/zs/age/18/sex/nan//使用函数助手不需要usethi…

了解详情 >>

Copyright @ 2008-2019   www.sznest.net   All rights reserved  网巢网是一家专业为企业提供免费建站、自助建站的模板建站公司     友情链接:香港张元洪律师行   

微信

移动端

客服

顶部