解决PHP中文乱码

来源:互联网 发布:光猫端口被关闭 编辑:程序博客网 时间:2024/05/24 00:23

zend studio的编辑器编码、php文件编码、网页页面编码、数据库编码应该保持一致。


1. php文件本身的编码与网页的编码应匹配 

        a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=gb2312">,所有文件的编码格式为ANSI,可用记事本打开,另存为选择编码为ANSI,覆盖源文件。 
        b. 如果欲使用utf-8编码,那么php要输出头:header(“Content-Type: text/html; charset=utf-8"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=utf-8">,所有文件的编码格式为utf-8。


比如

<pre name="code" class="php"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />//告诉浏览器用什么编码网页<title>你好</title></head><body><?phpheader("Content-Type: text/html; charset=utf-8");//告诉浏览器用什么编码php$user=$_GET["username"];$password=$_GET["passWord1"];echo "<p>"."用户名:".$user."</p>";//自己写的中文用什么编码由编辑器决定。echo "<P>"."密码是:".$password."</p>";?></body></html>





0 0