Home > php教程 > php手册 > body text

php 字符转码:php字符转码 utf-8 to gb2312

WBOY
Release: 2016-06-21 08:50:21
Original
985 people have browsed it

遇到php的转码问题,差点没折磨死我,无论我调用项目中的那些函数,都白做功夫,可能是掉错了,不知道,到网上找了好多,终于,终于,被我找到了:
iconv("UTF-8","gb2312",$username);
就是这个,我遇到的问题是这样的:
我要利用session的username,在config.php页,我可以得到,赋值给变量
$username=$_SESSION['user_name'];
$fromuser=$username;
然后要把这个值传到另一个页,再保存到数据库,从config.php到newmessage.php页就遇到麻烦了,

按上面的写法,值传到newmessage.php页就出现乱码了。说一下,我的整个系统是utf-8编码,而
config.php是gb2312(我也不清楚为什么把config.php编码直接改成utf-8就直接出现乱码,反正得是
gb2312或是gbk的),所以就需要转码了,把username在config.php直接转成gb2312,费了九牛二虎之力
好不容易找到这个疯狂代码,我的写法是这样的:
$fromuser=iconv("UTF-8","gb2312",$username);
腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress
返回的是数据格式为:var IPData = new Array(“114.218.183.139″,””,”江苏省”,”苏州市”);
使用JS代码进行调取:
查看源代码打印帮助
1 type="text/javascript"
src="http://fw.qq.com/ipaddress">
2
3 <script>document.write("你的IP是:"&#43;IPData[0]&#43;",来自:"&#43;IPData[2]);</script>
使用PHP解析的方法:
查看源代码打印帮助
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 ?>
处理后输出的是一个数组。
另:
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
本文链接http://www.cxybl.com/html/wlbc/Php/20130113/36055.html



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!