转换带有重音符号的外文字符
我正在尝试将一些文本与数据库中的文本进行比较。在数据库中,任何带重音的文本都会像 HTML 一样进行编码(即 é
),当我将数据库文本与我的字符串进行比较时,它不匹配,因为我的字符串只显示 é。当我首先使用 PHP 函数 htmlentities 对字符串进行编码时,é 变成了 é ,很奇怪吗?使用 htmlspecialchars 根本不会对 é 进行编码。
您建议我如何将 é 与 é
以及所有其他重音字符进行比较?
I'm trying to compare some text to the text in a database. In the database any text with an accent is encoded like in HTML (i.e. é
) when I compare the database text to my string it doesn't match because my string just shows é. When I use the PHP function htmlentities to encode the string first the é turns into é weird? Using htmlspecialchars doesn't encode the é at all.
How would you suggest I compare é to é
as well as all the other accented characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将正确的字符集发送到 htmlentities。看起来您使用的是 UTF-8,但默认值为 ISO-8859-1。像这样更改它:
另一种解决方案是在编码之前将文本转换为 ISO-8859-1,但这可能会破坏信息(ISO-8859-1 包含的字符数量几乎不及 UTF-8)。如果您想尝试这样做,请这样做:
You need to send in the correct charset to htmlentities. It looks like you're using UTF-8, but the default is ISO-8859-1. Change it like this:
Another solution is to convert the text to ISO-8859-1 before encoding, but that may destroy information (ISO-8859-1 does not contain nearly as many characters as UTF-8). If you want to try that instead, do like this:
我在法国网站上工作,我也遇到了同样的问题。这是我使用的功能。
它的作用是将字符串解码为 utf8,然后将所有内容转换为 HTML 实体。甚至标签。但是我们想要将标签转换回正常状态,那么 htmlspecialchars_decode 会将它们转换回来。所以最后你会得到一个带有转换后的重音符号的字符串,而无需接触标签。
您可以在将电子邮件内容发送给收件人之前使用此功能传递电子邮件内容。
您可能面临的另一个问题是,有时使用此函数,数据库中的内容会转换为 ? 。在这种情况下,您应该在运行查询之前执行此操作:
但您可能需要执行此操作,这取决于表中的编码。我希望它有帮助。
I'm working on french site, and I also had same problem. This is the function that I use.
What it does it decodes your string to utf8, than converts everything HTML entities. even tags. But we want to convert tags back to normal, than htmlspecialchars_decode will convert them back. So in the end you will get a string with converted accents without touching tags.
You can use pass through this function your email content before sending it to recipent.
Another issue you might face is that, sometimes with this function the content from database converts to ? . In this case you should do this before running your query:
But you might need to do it, it depends on encoding in your table. I hope it helps.
比较任务与您在创建数据库或表时选择的字符集和排序规则有关。如果您要保存带有很多口音的字符串(例如西班牙语),我建议您使用字符集 uft8,并且排序规则可能更适合您所使用的语言(英语、法语或其他语言)。
在数据库中使用正确的字符集的最好的事情是您可以以自然的方式保存字符串,例如:我的名字我可以将其存储为“Mario Juárez”,并且我不需要做一些奇怪的转换。
The comparing task is related to the charset and the collation you selected when you create the database or the tables. If you are saving strings with a lot of accents like spanish I sugget you to use charset uft8 and the collation could be the more accurate to the language(english, french or whatever) you're using.
The best thing of using the correct charset in the database is that you can save the string in natural way e.g: my name I can store it as is "Mario Juárez" and I have no need of doing some weird conversions.
最近遇到类似的问题。按照 Emil 的回答,它在本地运行良好,但在我们的开发/阶段环境中运行不佳。我最终使用了这个,它到处都有效:
感谢您引导我走向正确的方向!
Ran into similar issues recently. Followed Emil's answer and it worked fine locally but not on our dev/stage environments. I ended up using this and it worked all around:
Thanks for leading me in the right direction!