>  기사  >  php教程  >  PHP+AJAX中文乱码问题解决方法

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

WBOY
WBOY원래의
2016-06-21 09:08:09830검색

ajax|解决|问题|中文乱码

在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");



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.