js和php转换

2025-02-28 18:48:01
推荐回答(2个)
回答1:

这段代码是 rot-13 算法的JS实现,在php中有系统函数直接做到该算法: str_rot13

回答2:

/**
* 将unicode字符转换成普通编码字符
*
* @param string $str
* @param string $out_charset
* @return string
*/
function str_from_unicode($str, $out_charset = 'gbk'){
$str = preg_replace_callback("|&#([0-9]{1,5});|", 'unicode2utf8_', $str);
$str = iconv("UTF-8", $out_charset, $str);
return $str;
}

function unicode2utf8_($c){
return unicode2utf8($c[1]);
}
function unicode2utf8($c){
$str="";
if ($c < 0x80) {
$str.=$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;
}