php字符转码 utf-8 to gb2312
来源:互联网 发布:mac切换窗口的快捷键 编辑:程序博客网 时间:2024/06/13 16:04
遇到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接口地址:http://fw.qq.com/ipaddress
返回的是数据格式为:var IPData = new Array(“114.218.183.139″,””,”江苏省”,”苏州市”);
使用JS代码进行调取:
1
<script language=
"javascript"
type=
"text/javascript"
src=
"http://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
(
"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
- php字符转码 utf-8 to gb2312
- php字符转码 utf-8 to gb2312
- php字符转码 utf-8 to gb2312
- php字符转码 utf-8 gb2312
- php字符编码utf-8转gb2312的问题解决
- Java 字符转码之UTF-8转为GBK/GB2312
- java中GB2312 To Utf-8字符转换
- PHP Big5 Utf-8 GB2312 相互转码解决办法
- PHP Big5 Utf-8 GB2312 相互转码解决办法
- GB2312转UTF-8码
- linux gb2312 to utf-8
- GB2312转UTF-8
- UTF-8转GB2312
- UTF-8转GB2312
- utf-8转GB2312
- gb2312转utf-8
- gb2312 转 utf-8
- 字符编码 Unicode, ANSI, GBK, GB2312,UTF-8,区位码,
- linux终端中输出彩色字体
- EXT2的文件操作方法
- Swing之图片水平翻转、垂直翻转、180度旋转
- 左手IT,右手CT
- Swing之拖动篇:将文本中的内容拖动到表中
- php字符转码 utf-8 to gb2312
- Swing之JTree篇:在树节点前增加JCheckBox控件
- EXT2普通文件节点的操作方法
- Q&A : 腾讯的核心竞争力
- 排序的之插入排序
- Swing之JTree篇:设置树选中节点背景色
- EXT2目录文件索引节点的操作方法
- 代码调试技巧
- Key-Value Database调研之Riak篇