utf8_encode 和重音字符的困境?
我在使用 utf8_encode
解码时面临着一个悖论。我有一个具有 uft8 排序规则的 MySQL 数据库,其字段具有 utf8_general
编码。我的 php 文件采用 utf8 格式,并且在 HTML 页面中,我在标头中指定了 utf8 字符集。
我的问题是,当我从表中选择一个包含重音字符的字段(例如 èçò ùé)并将其回显到浏览器时,我会得到奇怪的字符。
要解决我的问题,我必须 echo $description=utf8_encode($imm['description'])
。
我的问题是为什么我不能直接执行 echo 而不必每次都使用 uft8_encode
?
I am facing a paradox in decoding with utf8_encode
decode. I have a MySQL database with uft8 collation and whose fields have the utf8_general
coding. I have my php file in utf8, and in my HTML pages I have specified in the header the utf8 charset.
My problem is that when I select from my table a field containing accented characters (like èçò ùé) and echo that to the browser, I get strange characters.
To resolve my problem, I have to echo $description=utf8_encode($imm['description'])
.
My question is why can’t I do the echo directly without having to use uft8_encode
every time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是猜测您的数据库连接未设置为 UTF-8。
请参阅 在 MySQL 中设置名称 utf8?
I'll just guess that your database connection is not set to UTF-8.
See SET NAMES utf8 in MySQL?
您需要使用 php 将标头指定为 utf-8。还要确保在存储到数据库之前字符的格式为 utf-8,因为 utf_encode 将 ISO-8859-1 字符串编码为 UTF-8,这很可能意味着字符将作为 ISO-8859-1 存储在sa utf-8 表。
确保在将这些字符存储到数据库之前将它们转换为 utf-8,然后 echo 根本不成问题。
来源:我自己也遇到了完全相同的问题。
you need to specify the header using php to be utf-8. also make sure that the format of the chars is utf-8 before storing in the db because utf_encode encodes an ISO-8859-1 string to UTF-8, which most likely means that the chars are being stored as ISO-8859-1 in s a utf-8 table.
make sure that you convert those chars in utf-8 before storing them in the db and then echo should not be a problem at all.
Source: had the exact same problem myself.