php 如何转换这些奇怪的字符?(??, ?, ??, ?1, ?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5127744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:52:58  来源:igfitidea点击:

How to convert these strange characters? (??, ?, ??, ?1, ?)

phpmysqlcharacter-encodingmojibakeutf8-decode

提问by Leonardo

My page often shows things like ??, ?, ??, ?1, ? in place of normal characters.

我的页面经常显示诸如 ??, ?, ??, ?1, ? 代替正常字符。

I use utf8 for header page and MySQL encode. How does this happen?

我使用 utf8 作为标题页和 MySQL 编码。这是怎么发生的?

回答by Ray

These are utf-8 encoded characters. Use utf8_decode()to convert them to normal ISO-8859-1 characters.

这些是 utf-8 编码的字符。使用utf8_decode()将它们转换为正常的 ISO-8859-1 字符。

回答by Gumbo

If you see those characters you probably just didn't specify the character encoding properly. Because those characters are the result when an UTF-8 multi-byte string is interpreted with a single-byte encoding like ISO 8859-1or Windows-1252.

如果您看到这些字符,您可能只是没有正确指定字符编码。因为这些字符是使用单字节编码(如ISO 8859-1Windows-1252)解释 UTF-8 多字节字符串时的结果。

In this case ??could be encoded with 0xC3 0xAB that represents the Unicode character ?(U+00EB) in UTF-8.

在这种情况下,??可以使用代表?UTF-8 中的 Unicode 字符(U+00EB) 的0xC3 0xAB 进行编码。

回答by davidkonrad

Even though utf8_decodeis a useful solution, I prefer to correct the encoding errors on the table itself. In my opinion it is better to correct the bad characters themselves than making "hacks" in the code. Simply do a replaceon the field on the table. To correct the bad encoded characters from OP :

尽管utf8_decode是一个有用的解决方案,但我更喜欢更正表本身的编码错误。在我看来,纠正坏字符本身比在代码中进行“黑客攻击”要好。只需replace在桌子上的字段上做一个。要更正来自 OP 的错误编码字符:

update <table> set <field> = replace(<field>, "??", "?")
update <table> set <field> = replace(<field>, "?", "à")
update <table> set <field> = replace(<field>, "??", "ì")
update <table> set <field> = replace(<field>, "?1", "ù")

Where <table>is the name of the mysql table and <field>is the name of the column in the table. Here is a very good check-list for those typically bad encoded windows-1252 to utf-8 characters -> Debugging Chart Mapping Windows-1252 Characters to UTF-8 Bytes to Latin-1 Characters.

其中<table>是mysql表<field>的名称,是表中列的名称。对于那些通常编码错误的 windows-1252 到 utf-8 字符 ->调试图表映射 Windows-1252 字符到 UTF-8 字节到拉丁 1 字符,这是一个非常好的检查表。

Remember to backup your table before trying to replace any characters with SQL!

请记住在尝试用 SQL 替换任何字符之前备份您的表

[I know this is an answer to a very old question, but was facing the issue once again. Some old windows machine didnt encoded the text correct before inserting it to the utf8_general_ci collated table.]

[我知道这是一个非常古老的问题的答案,但再次面临这个问题。一些旧的 Windows 机器在将文本插入到 utf8_general_ci 整理表之前没有正确编码文本。]