php中文乱码问题解决方法
1.mysql php中文乱码
出现这种问题是数据库连接与网页编码不统一导致了
解决办法
在mysql 连接时我们在后面设置一句
mysql_query("SET NAMES GBK");
例子
在连接数据库的语句中.
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6463')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6463>mysql_connect('localhost','user','password');
mysql_select_db('my_db');
mysql_query("set names 'utf8'"); //select 数据库之后加多这一句
2.PHP中文文件名输出乱码
出现文名乱码是我们环境不支持中文所导致了或编码问题我们只要进行转换字符编码为uft8即可
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6948')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6948>$file_name="我的文件.jpg";
$file_name=iconv("utf-8","gb2312",$file_name); //解决中文乱码问题
echo '$file_name';
3.json_encode中文乱码问题
1.json_encode UTF8码中文后的字符串不可阅读
2.json_encode 多级数组中文乱码问题
3.json_encode 数组中包含换行时错误问题
4.json_encode 数组中键为中文的问题
function _encode($arr)
{
$na = array();
foreach ( $arr as $k => $value ) {
$na[_urlencode($k)] = _urlencode ($value);
}
return addcslashes(urldecode(json_encode($na)),"rn");
}
function _urlencode($elem)
{
if(is_array($elem)){
foreach($elem as $k=>$v){
$na[_urlencode($k)] = _urlencode($v);
}
return $na;
}
return urlencode($elem);
}
3.HTML内容编码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />,这里我们指定了浏览器在解析HTML时使用UTF-8编码 。如果这里没有指定,浏览器会使用自己缺省的编码。不同的浏览器缺省编码会有所不同,比如IE6是GB2312,FireFox是UTF-8。所以,上面这段代码如果没有,在FireFox会显示正常,在IE6就会显示乱码。
4.php内容乱码
在php中我们只要在文件头加上
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3064')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3064><?php
header("Content-type:text/html;charset=utf-8");
?>
5.url中文乱码的兼容性
在url地址里出现中文参数,接收页面出现乱码,这个是经常遇到的问题
最基本的解决办法是链接里加上urlencode,接收页面会自动解码或者加上urldecode
但是有时候链接来自多个地方,有的意见urlencode了,有点没有
比如在谷歌浏览器(chrome)地址栏里敲入中文,传递的过程浏览器会自动给你编码
而IE核心缺不然
这就导致同一个接收页面在谷歌浏览器里可以正常显示,而在IE浏览器则是乱码。
解决方法:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2767')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2767>$keyboard2 = iconv("gb2312","UTF-8",$keyboard);
if($keyboard2)
$keyboard=$keyboard2;
6.htmlentities汉字中文乱码
htmlentities函数作用在汉字变量中的时候会出现乱码
$resultsText = str_replace("[QUERY]", htmlentities($(www.111cn.net)query), $resultsText);
正确的做法是改变htmlentities的默认参数
htmlentities($query,ENT_COMPAT,'UTF-8')
7.PHP下载文件名中的中文乱码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7296')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7296>$filename = "中文 文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace(" ", " ", $encoded_filename);
您可能感兴趣的文章:
php网页标题中文乱码如何解决
php分割GBK中文乱码的解决方法
php pdo编码乱码问题的解决方法
解决php json_encode中文转码、乱码问题
php截取中文字符串乱码如何解决呢
mysql中utf-8编码数据库转换成GBK编码的问题
php读取mysql中文乱码解决方法
MySQL与phpMyAdmin中文乱码的解决方法
php连接mysql中文乱码的解决方法
php输出汉字乱码怎么解决?