php怎么实现ucs2编码
2022-12-02 09:01:16
197
{{single.collect_count}}

php实现ucs2编码的方法:1、创建一个PHP示例文件;2、通过“function Ucs2Code($str,$encode="UTF-8"){...}”方法实现ucs2编码即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

本文操作环境:Windows7系统、PHP7.1版、Dell G3电脑。

php怎么实现ucs2编码?

PHP实现UCS2编码解码 手机发送短信时编码

代码如下:

/**** @Method Ucs2Code UCS2编码* @Param $str 输入字符串* @Param $encod 输入字符串编码类型(UTF-8,GB2312,GBK)* @Return 返回编码后的字符串*/function Ucs2Code($str,$encode="UTF-8"){$jumpbit=strtoupper($encode)=='GB2312'?2:3;//跳转位数$strlen=strlen($str);//字符串长度$pos=0;//位置$buffer=array();for($pos=0;$pos<$strlen;){if(ord(substr($str,$pos,1))>=0xa1){//0xa1(161)汉字编码开始$tmpChar=substr($str,$pos,$jumpbit);$pos+=$jumpbit;}else{$tmpChar=substr($str,$pos,1);++$pos;}$buffer[]=bin2hex(iconv("UTF-8","UCS-2",$tmpChar));}return strtoupper(join("",$buffer));}/**** @Method unUcs2Code UCS2解码* @Param $str 输入字符串* @Param $encod 输入字符串编码类型(UTF-8,GB2312,GBK)* @Return 返回解码后的字符串*/function unUcs2Code($str,$encode="UTF-8"){$strlen=strlen($str);$step=4;$buffer=array();for($i=0;$i<$strlen;$i+=$step){$buffer[]=iconv("UCS-2",$encode,pack("H4",substr($str,$i,$step)));}return join("",$buffer);}echo Ucs2Code("进入围栏");echo unUcs2Code("8FDB516556F4680F");
登录后复制

推荐学习:《PHP视频教程

以上就是php怎么实现ucs2编码的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}