Home > Article > Backend Development > PHP character transcoding utf-8 to gb2312_PHP tutorial
I encountered a PHP transcoding problem that almost tortured me to death. No matter what functions I called in the project, all my efforts were in vain. Maybe it was a mistake. I don’t know. I searched a lot on the Internet. Finally, finally, I found it. Now:
iconv("UTF-8","gb2312",$username);
This is it, the problem I encountered is this:
I want to use the username of the session. On the config.php page, I can get it and assign it to the variable
$username=$_SESSION['user_name'];
$fromuser=$username;
Then you need to transfer this value to another page and save it to the database. You will encounter trouble when going from config.php to newmessage.php page,
According to the above writing method, the value will be garbled when it is passed to the newmessage.php page. By the way, my entire system is utf-8 encoded, and
config.php is gb2312 (I don’t know why garbled characters appear immediately after changing the encoding of config.php directly to utf-8. Anyway, it must be
gb2312 or gbk), so it needs to be transcoded. Convert the username in config.php directly to gb2312, which took a lot of effort
I finally found this crazy code. Here’s how I wrote it:
$fromuser=iconv("UTF-8","gb2312",$username);
================================================== =============
Tencent’s IP address API interface address: http://fw.qq.com/ipaddress
The returned data format is: var IPData = new Array("114.218.183.139″,"","Jiangsu Province","Suzhou City");
Use JS code to call:
View source code print help
1
2
3 <script>document.write("Your IP is: "+IPData[0]+", from: "+IPData[2]);</script>
Method of parsing using PHP:
View source code print help
01
02 function getIpPlace(){
03 $ip=file_get_contents("http://fw.qq.com/ipaddress");
04 $ip=str_replace('"',' ',$ip);
05 $ip2=explode("(",$ip);
06 $a=substr($ip2[1],0,-2);
07 $b=explode(",",$a);
08 return $b;
09 }
10 $ip=getIpPlace();
11 print_r($ip);
12 ?>
The output after processing is an array.
Also:
Sina’s IP address query interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
Sina multi-region testing method: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
Sohu IP address query interface (default GBK): http://pv.sohu.com/cityjson
Sohu IP address query interface (encoding can be set): http://pv.sohu.com/cityjson?ie=utf-8
Sohu’s other IP address query interface: http://txt.go.sohu.com/ip/soip
Excerpted from Hurry’s column