php unicode乱码加密,PHP解密Unicode及Escape加密字符串

这篇博客介绍了如何使用PHP解密Unicode和Escape加密的字符串。提供的两个函数`unicode2utf8`用于将Unicode编码转换为UTF-8,而`phpescape`则将字符串从GBK编码转为Unicode并进行Escape编码。示例中展示了如何解密和加密中文字符,如'大家好,我是孤魂!这是测试文本!'
摘要由CSDN通过智能技术生成

PHP解密Unicode及Escape加密字符串

function unicode2utf8($c) {

$str="";

if ($c < 0x80) {

$str.=chr($c);

} else if ($c < 0x800) {

$str.=chr(0xc0 | $c>>6);

$str.=chr(0x80 | $c & 0x3f);

} else if ($c < 0x10000) {

$str.=chr(0xe0 | $c>>12);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

} else if ($c < 0x200000) {

$str.=chr(0xf0 | $c>>18);

$str.=chr(0x80 | $c>>12 & 0x3f);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

}

return $str;

}

$str='%u5927%u5BB6%u597D%uFF0C我是孤魂!
\u8FD9\u662F\u6D4B\u8BD5\u6587\u672C\uFF01';

echo uni_decode($str); // 大家好,我是孤魂!这是测试文本!

在网上搜索一把,很多用php实现的escape函数,大同小异

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function phpescape($str){

preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr);

$ar = $newstr[0];

foreach($ar as $k=>$v){

if(ord($ar[$k])>=127){

$tmpString=bin2hex(iconv("GBK","ucs-2",$v));

if (!eregi("WIN",PHP_OS)){

$tmpString = substr($tmpString,2,2).substr($tmpString,0,2);

}

$reString.="%u".$tmpString;

} else {

$reString.= rawurlencode($v);

}

}

return $reString;

}

【PHP解密Unicode及Escape加密字符串】相关文章:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值