Heim  >  Artikel  >  Backend-Entwicklung  >  php字符转码 utf-8 to gb2312_PHP教程

php字符转码 utf-8 to gb2312_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:47:161360Durchsuche

 

遇到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    

2    

3     <script>document.write("你的IP是:"+IPData[0]+",来自:"+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

 

摘自 Hurry的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478525.htmlTechArticle遇到php的转码问题,差点没折磨死我,无论我调用项目中的那些函数,都白做功夫,可能是掉错了,不知道,到网上找了好多,终于,终于...
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