Home  >  Article  >  Backend Development  >  How to solve the problem of garbled values ​​in php ajax

How to solve the problem of garbled values ​​in php ajax

藏色散人
藏色散人Original
2020-09-01 10:31:352409browse

php The solution to the garbled value passed by ajax: 1. Use UTF8 encoding for the front and back pages and the database; 2. Use the "mysql_query("set names gb2312");" method when saving to the database; 3. , Use the ICONV function to convert the encoding of the string taken from the database.

How to solve the problem of garbled values ​​in php ajax

Recommendation: "PHP Video Tutorial"

In PHP website development, in order to increase user experience, Ajax It is a frequently used technology, but for beginners, they often encounter the problem of garbled Chinese characters when Ajax transfers values. Is there any way to solve the problem of garbled Chinese characters when PHP AJAX transfers values?

We know that Ajax technology evolved from JavaScript, and JavaScript uses UTF-8 encoding. When the current background page uses GBK or other encoding, and no encoding conversion is performed, the problem of garbled Chinese characters will occur.

PHP Ajax value transfer Chinese character garbled solution

Method 1. The front and back pages and database use UTF8 encoding uniformly. This is the simplest method.

Method 2: When the website has adopted encoding such as GBK/GB2312, there are two situations:

1. When Ajax sends Chinese characters and PHP (backend program) receives garbled characters, Use the conversion function between GBK and UTF8 to convert the encoding of the received string, and then store it in the relevant database. It is assumed that the encoding used by the database is GBK or GB2312. If the configured PHP running environment supports the ICONV function, you can also use the ICONV function. Encoding conversion, and then

mysql_query("set names gb2312");

can solve the Chinese garbled problem of Ajax value transfer when saving to the database.

 2. When PHP sends Chinese characters and Ajax (front page) receives garbled characters, you can also use the ICONV function to convert the encoding of the string taken from the database, and then pass the value to the Ajax front desk, that is, responseText . Or add

header('Content-type: text/html;charset=gb2312');

before the PHP (backend program) output character stream. Special point out: When using Ajax to do multi-level linkage (such as province-city linkage), use the XML interactive form, and you must also add a header before outputting XML.

At the same time, the conversion function of GB2312 and UTF8 is posted, so that everyone can have another solution when encountering the problem of Ajax Chinese garbled characters.

function gb2utf8($gb,$filename)
{
    if(!trim($gb))
    return $gb;
    //$filename="gb2312.txt";
    $tmp=file($filename);
    $codetable=array();
    while(list($key,$value)=each($tmp))
    $codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
    $utf8="";
    while($gb)
    {
        if (ord(substr($gb,0,1))>127)
        {
            $thisgb=substr($gb,0,2);
            $gb=substr($gb,2,strlen($gb));
            $utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($thisgb))-0x8080]));
        }
        else
        {
            $gb=substr($gb,1,strlen($gb));
            $utf8.=u2utf8(substr($gb,0,1));
        }
    }
    $ret="";
    for($i=0;$i>6);
        $str.=(0x80 $c & 0x3F);
    }
    else if ($c < 0x10000) {
        $str.=(0xE0 $c>>12);
        $str.=(0x80 $c>>6 & 0x3F);
        $str.=(0x80 $c & 0x3F);
    }
    else if ($c < 0x200000) {
        $str.=(0xF0 $c>>18);
        $str.=(0x80 $c>>12 & 0x3F);
        $str.=(0x80 $c>>6 & 0x3F);
        $str.=(0x80 $c & 0x3F);
    }
    return $str;
}

Note: This function requires the use of GB2312 Chinese encoding table, please click here to download. Since gb2312 is used, some rare words may be garbled during conversion.

The above is the detailed content of How to solve the problem of garbled values ​​in php ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn