PHP 字符集重音问题
我的页面中有一个表单供用户发表评论。
我当前正在使用此字符集:
meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"
但从数据库重音检索注释未显示正确( Ex. è =>è )。
为了正确处理重音,我应该关心哪些参数?
已解决
将元标记更改为 charset='utf-8'
更改了 Mysql 的字符集(ALTER TABLE comments CONVERT TO CHARACTER SET utf-8)
更改了连接字符集插入记录和检索时 ($conn->query('SET NAMES utf8'))
现在口音显示正确
谢谢卢卡
,
I have a form in my page for users to leave a comment.
I'm currently using this charset:
meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"
but retrieveving the comment from DB accents are not displaying correct ( Ex. è =>è ).
Which parameters should i care about for a correct handling of accents?
SOLVED
changed meta tag to charset='utf-8'
changed character-set Mysql (ALTER TABLE comments CONVERT TO CHARACTER SET utf-8)
changed connection character-set both when inserting records and retrieving ($conn->query('SET NAMES utf8'))
Now accents are displaying correct
thanks
Luca
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当涉及到 LAMP Web 应用程序时,字符集可能很复杂并且调试起来很痛苦。在一个软件与另一个软件对话的每个阶段,都可能存在错误的字符集转换或错误的数据存储。
您需要注意的地方是:
- 在浏览器和网络服务器之间(您已经列出)
- PHP 和 MySQL 服务器之间
您列出的字符看起来像普通的欧洲字符,该字符将包含在 ISO-8859-1 字符集中。
要检查的事项:
您没有说您是否使用同一个网络应用程序或其他客户端从数据库中读取数据。我建议您尝试使用相同的网络应用程序并使用相同的元字符集标头来读取它(再次检查浏览器是否确实设置了它)并查看浏览器中显示的内容。
要调试这些问题,需要您真正确定您正在使用的客户端/控制台是否也在进行任何转换,最安全的方法有时是将数据输入十六进制编辑器,在其中您可以确保没有其他任何事情乱七八糟与任何翻译。
如果它看起来不像是浏览器端问题,请您在数据库中包含以下命令的输出:
从您的 Web 应用程序建立的连接运行(而不是从其他 MySQL 客户端):
从任何 MySQL 运行客户端:(
其中 myTable 是您正在从中读取/写入数据的表)
Character sets can be complicated and pain to debug when it comes to LAMP web applications. At each of the stages that one piece of software talks to another there's scope for incorrect charset translation or incorrect storage of data.
The places you need to look out for are:
- Between the browser and the web server (which you've listed already)
- Between PHP and the MySQL server
The character you've listed look like normal a European character that will be included in the ISO-8859-1 charset.
Things to check for:
You've not said if you're reading data from the database back out with the same web-app or with some other client. I'd suggest you try to read it out with the same web application and using the same meta charset header (again, check the browser is really setting it) and see what is displayed in the browser.
To debug these issues requires you to be really sure about whether the client/console you're using is doing any conversion too, the safest way is sometimes to get the data into a hex editor where you can be sure that nothing else is messing around with any translation.
If it doesn't look like it's a browser-side problem please can you include the output of the following commands against your database:
Run from a connection that your web-app makes (not from some other MySQL client):
Run from any MySQL client:
(where myTable is the table you're reading/writing data from/to)
ISO-8859-1 字符集仅适用于拉丁字符。尝试使用 UTF-8,并确保这些字符来自的数据库也是 UTF-8 列。
The ISO-8859-1 character set is for Latin characters only. Try UTF-8, and make sure that the database these characters are coming from are also UTF-8 columns.