首页 > 开发 > PHP > 正文

PHP+AJAX中文乱码问题解决方法

2024-05-04 22:58:11
字体:
来源:转载
供稿:网友

在php从ajax的来的数据进行转化函数

function utf8rawurldecode ($source) {
    
$decodedstr = ""
;
    
$pos = 0
;
    
$len = strlen ($source
);
     while (
$pos < $len
) {
        
$charat = substr ($source, $pos, 1
);
         if (
$charat == '%'
) {
            
$pos
++;
            
$charat = substr ($source, $pos, 1
);
             if (
$charat == 'u'
) {
                
// we got a unicode character
                
$pos
++;
                
$unicodehexval = substr ($source, $pos, 4
);
                
$unicode = hexdec ($unicodehexval
);
                
$entity = "&#". $unicode . ';'
;
                
$decodedstr .= utf8_encode ($entity
);
                
$pos += 4
;
             }
             else {
                
// we have an escaped ascii character
                
$hexval = substr ($source, $pos, 2
);
                
$decodedstr .= chr (hexdec ($hexval
));
                
$pos += 2
;
             }
         } else {
            
$decodedstr .= $charat
;
            
$pos
++;
         }
     }
     return
$decodedstr
;
}

运用以下函数

$formname=utf8rawurldecode($formname);

iconv("utf-8","gb2312",$formname);


因为ajax通过escape转换数据加密了数据

================================================

查资料显示可以用

$formname=mb_convert_encoding($formname,"gb2312","utf-8");

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表