Home  >  Article  >  Backend Development  >  Solve the php mysql garbled problem_PHP tutorial

Solve the php mysql garbled problem_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:52:11706browse

本文的作用是为了全方位的避免因使用UTF-8编码而产生的乱码问题,不包含具体的乱码解决方案和编码转换的内容。对于UTF-8编码的深入了解,请参阅:《PHP匹配UTF-8中文字符的正则表达式》
1、编辑器:不要使用任何微软的编辑器,什么Frontpage,Web Designer,记事本,写字板什么的,能丢的全部丢开,因为这些编辑器会在你的UTF-8文档前面产生BOM,关于BOM的具体说明,可以在 这里 找到,当年我直接拿记事本转UTF-8覆盖原文件,造成大量代码损毁,至今记忆犹新。
2、MySQL数据库:注意建库,建表,建字段(注意有三处)的时候,都要选用utf8_general_ci的整理格式(Collation),在PHP使用mysql_connect()函数连接数据之后,需要加上一句:
mysql_query('set names "utf8"');
3、PHP:
使用mb库,不要使用iconv库。
使用preg而不要使用ereg来处理字符。
使用htmlentities()函数,html_entity_decode()函数的时候要带上第三个参数:

$str = "中文";
echo htmlentities($str,ENT_COMPAT,"UTF-8"); //显示(源代码里面) <a href='test'>涓?

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632493.htmlTechArticle本文的作用是为了全方位的避免因使用UTF-8编码而产生的乱码问题,不包含具体的乱码解决方案和编码转换的内容。对于UTF-8编码的深入了解...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn