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

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

WBOY
WBOYOriginal
2016-06-21 09:08:09830Durchsuche

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



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn