当前位置: 移动技术网 > IT编程>开发语言>PHP > php字符转码 utf-8 to gb2312

php字符转码 utf-8 to gb2312

2019年06月19日  | 移动技术网IT编程  | 我要评论

 

遇到php的转码问题,差点没折磨死我,无论我调用项目中的那些函数,都白做功夫,可能是掉错了,不知道,到网上找了好多,终于,终于,被我找到了:

 

iconv("utf-8","gb2312",$username);

 

就是这个,我遇到的问题是这样的:

 

我要利用session的username,在config.php页,我可以得到,赋值给变量

 

$username=$_session['user_name'];

 

$fromuser=$username;

 

然后要把这个值传到另一个页,再保存到,从config.php到newmessage.php页就遇到麻烦了,

 

<input type="hidden" name="fromuser" id="fromuser" value="<?php echo $fromuser; ?>"/>

 

按上面的写法,值传到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接口地址:https://fw.qq.com/ipaddress

 

返回的是数据格式为:var ipdata = new array(“114.218.183.139′,””,”江苏省”,”苏州市”);

 

使用js代码进行调取:

 

查看源代码打印帮助

1     <script language="javascript" type="text/javascript" src="https://fw.qq.com/ipaddress"></script>

2    

3     <script>document.write("你的ip是:"+ipdata[0]+",来自:"+ipdata[2]);</script>

使用php解析的方法:

 

查看源代码打印帮助

01    <?php

02    function getipplace(){

03    $ip=file_get_contents("https://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地址查询接口:https://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

 

新浪多地域测试方法:https://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

 

搜狐ip地址查询接口(默认gbk):https://pv.sohu.com/cityjson

 

搜狐ip地址查询接口(可设置编码):https://pv.sohu.com/cityjson?ie=utf-8

 

搜狐另外的ip地址查询接口:https://txt.go.sohu.com/ip/soip

 

摘自 hurry的专栏

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网