转换带有重音符号的外文字符

发布于 2024-11-03 20:33:51 字数 261 浏览 0 评论 0原文

我正在尝试将一些文本与数据库中的文本进行比较。在数据库中,任何带重音的文本都会像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最佳男配角 2024-11-10 20:33:51

您需要将正确的字符集发送到 htmlentities。看起来您使用的是 UTF-8,但默认值为 ISO-8859-1。像这样更改它:

$encoded = htmlentities($text, ENT_COMPAT, 'UTF-8');

另一种解决方案是在编码之前将文本转换为 ISO-8859-1,但这可能会破坏信息(ISO-8859-1 包含的字符数量几乎不及 UTF-8)。如果您想尝试这样做,请这样做:

$encoded = htmlentities(utf8_decode($text));

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:

$encoded = htmlentities($text, ENT_COMPAT, 'UTF-8');

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:

$encoded = htmlentities(utf8_decode($text));
丶情人眼里出诗心の 2024-11-10 20:33:51

我在法国网站上工作,我也遇到了同样的问题。这是我使用的功能。

function convert_accent($string)
{
    return htmlspecialchars_decode(htmlentities(utf8_decode($string)));
}

它的作用是将字符串解码为 utf8,然后将所有内容转换为 HTML 实体。甚至标签。但是我们想要将标签转换回正常状态,那么 htmlspecialchars_decode 会将它们转换回来。所以最后你会得到一个带有转换后的重音符号的字符串,而无需接触标签。
您可以在将电子邮件内容发送给收件人之前使用此功能传递电子邮件内容。

您可能面临的另一个问题是,有时使用此函数,数据库中的内容会转换为 ? 。在这种情况下,您应该在运行查询之前执行此操作:

mysql_query("SET NAMES `utf8`");

但您可能需要执行此操作,这取决于表中的编码。我希望它有帮助。

I'm working on french site, and I also had same problem. This is the function that I use.

function convert_accent($string)
{
    return htmlspecialchars_decode(htmlentities(utf8_decode($string)));
}

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:

mysql_query("SET NAMES `utf8`");

But you might need to do it, it depends on encoding in your table. I hope it helps.

演出会有结束 2024-11-10 20:33:51

比较任务与您在创建数据库或表时选择的字符集和排序规则有关。如果您要保存带有很多口音的字符串(例如西班牙语),我建议您使用字符集 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.

千寻… 2024-11-10 20:33:51

最近遇到类似的问题。按照 Emil 的回答,它在本地运行良好,但在我们的开发/阶段环境中运行不佳。我最终使用了这个,它到处都有效:

$title = html_entity_decode(utf8_decode($item));

感谢您引导我走向正确的方向!

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:

$title = html_entity_decode(utf8_decode($item));

Thanks for leading me in the right direction!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文