How to solve the character garbled in PHP's Ajax transfer value

Source: Internet
Author: User
Tags header php web development php website return strlen utf 8

In the PHP website development, in order to increase the user experience, Ajax is a commonly used technology, but for beginners, often encounter Ajax to pass the value of Chinese strings into garbled problem, what method can solve the PHP Ajax value in the text character garbled problem?

We know that Ajax technology evolved from JavaScript, and JavaScript using UTF-8 encoding, the current background page with GBK or other encoding, and without the encoding conversion, there will be a Chinese character garbled problem.

The method of writing character garbled in PHP Ajax transfer value

Method one, front and back page and database unified using UTF8 code, this is the simplest way.

Method Two: When the website has already adopted such as gbk/gb2312 coding, it is divided into three cases.

1, Ajax send Chinese characters, PHP (background program) to receive garbled, using GBK and UTF8 of the conversion function of the accepted string encoding conversion, and then into the relevant database, where the assumption that the database is GBK or GB2312 code, If the configured PHP operating environment supports the ICONV function, you can also use the ICONV function to encode the conversion, and then when you save the database

1
mysql_query ("Set names gb2312");

Can solve the Ajax transfer value Chinese garbled problem.

2, PHP send Chinese characters, Ajax (front page) to receive garbled, the same can use the Iconv function of the string extracted from the database code conversion, and then pass the value to the Ajax foreground, that is responsetext. or the PHP (background program) output character stream before adding

1
Header (' content-type:text/html;charset=gb2312 ');

Can. In particular: in the use of AJAX multi-level linkage (such as the provincial city linkage), in the form of XML interaction, before the output of XML must also be preceded by header.

At the same time, the GB2312 and UTF8 of the conversion function , convenient for everyone in the encounter Ajax Chinese garbled problem one more solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);


$utf 8 = "";


while ($GB)


    {


if (Ord (substr ($GB, 0,1)) >127)


        {


$THISGB =substr ($GB, 0,2);


$GB =substr ($GB, 2,strlen ($GB));


$utf 8.=u2utf8 (Hexdec ($codetable [Hexdec (Bin2Hex ($THISGB)) -0x8080]);


        }


Else


        {


$GB =substr ($GB, 1,strlen ($GB));


$utf 8.=u2utf8 (substr ($GB, 0, 1));


        }


    }


$ret = "";


for ($i =0; $i <strlen ($utf 8); $i +=3)


$ret. =CHR (substr ($utf 8, $i, 3));


return $ret;


}





function U2utf8 ($c)


{


for ($i =0; $i <count ($c); $i + +)


$str = "";


if ($c < 0x80) {


$str. = $c;


    }


else if ($c < 0x800) {


$str. = (0xc0 $c >>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 needs to use GB2312 Chinese Code table, please click here to download, because it is using gb2312, for some relatively uncommon words in the conversion may appear garbled.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.