sm702 发表于 2015-11-17 11:21:51

php编程,打印url传过来的中文是乱码

url编码
语法: string urlencode(string str);
返回值: 字符串
函数种类: 编码处理
例如:

复制代码代码如下:
<?php
$ChineseName=&quot;我的名字,是中文的哦&quot;;
$EncodeStr=urlencode($ChineseName);
echo &quot;<a href=/cgi/personal.cgi?name=$EncodeStr>我的名字</a>&quot;;
?>

url解码
还原 URL 编码字符串。
语法: string urldecode(string str);
返回值: 字符串
函数种类: 编码处理
例如:
对前面传过来的中文进行处理显示
复制代码代码如下:
<?php
$DecodeStr=urldecode($_GET['name']);//你可能不用解码都可以,因为浏览器会自动帮你解码
echo $DecodeStr;
?>

关于php用get方法从url上获得的中文乱码问题

使用$gonghui = iconv(&quot;gb2312&quot;,&quot;UTF-8&quot;,$gonghui);另一方法代码
/**
* 多字节字符串编码转换函数
*
* @param string str 需要进行编码转换的字符串
* @param string to_encoding 指定转换为某种编码,如:gb2312、gbk、utf-8等
* @param mixed from_encoding 混合指定原来字串的编码,如:同时指定 JIS, eucjp-win, sjis-win 混合编码
* @return string
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
**/
mb_convert_encoding 函数为php内部多字节字符串编码转换函数,可以在有需要的使用场合,几乎支持所有编码。PHP >= 4.0.6、 5 版本支持。
直接获取 reg.php?gh=某某;
//工会登入参
$gonghui = $_GET['gh'];
获得的$gonghui 为gb2312编码 输出到utf-8网页上显示乱码
改成
//工会登入参数
$gonghui = $_GET['gh'];
$gonghui = mb_convert_encoding($gonghui, &quot;UTF-8&quot;, &quot;gb2312&quot;);
就显示正常了
对整个页面进行转换
该方法适用所有编码环境。这样把前128个字符以外(显示字符)的字符集都用 NCR(Numeric character reference,如“汉字”将转换成“汉字”这种形式)来表示,这样的编码在任意编码环境下页面都能正 常显示。
在php文件的头部加上下面三行代码:

复制代码代码如下:
mb_internal_encoding(&quot;gb2312&quot;); // 这里的gb2312是你网站原来的编码
mb_http_output(&quot;HTML-ENTITIES&quot;);
ob_start('mb_output_handler');

使用mb_convert_encoding 函数需启用PHP 的mbstring (multi-byte string)扩展。
如果没有没有开启php的mbstring扩展,则需要做如下设置,让php支持该扩展。
1、windows 服务器环境
编辑 php.ini 文件,将; extension=php_mbstring.dll 前面的 ; 去掉,重启网页服务器。
2、Linux服务器环境
在编译配置时加入 --enable-mbstring=cn 编译参数,再进行PHP的编译安装。
其它网友的第三个参考方法:
//方法一 urldecode
$url = 'aaa.php?region='.urldecode(&quot;四川省&quot;);
<a href=&quot;<?php echo $url;?>&quot;>aaa </a>
//方法二base64_encode
<?
$test=&quot;四川省&quot;;
$test1=base64_encode($test);
echo '<a href=&quot;www.iyunv?region=$test1&quot;>aaa </a>';
?>
另一页面使用base64_decode解开
base64_decode($region);
//方法三让服务器支持中文
# locale
lang=zh_cn.utf-8
lc_ctype=&quot;zh_cn.utf-8&quot;
lc_numeric=&quot;zh_cn.utf-8&quot;
lc_time=c
lc_collate=c
lc_monetary=&quot;zh_cn.utf-8&quot;
lc_messages=&quot;zh_cn.utf-8&quot;
lc_paper=&quot;zh_cn.utf-8&quot;
lc_name=&quot;zh_cn.utf-8&quot;
lc_address=&quot;zh_cn.utf-8&quot;
lc_telephone=&quot;zh_cn.utf-8&quot;
lc_measurement=&quot;zh_cn.utf-8&quot;
lc_identification=&quot;zh_cn.utf-8&quot;
lc_all=
#
         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: php编程,打印url传过来的中文是乱码