用重音转换外来字符 [英] Convert foreign characters with accents

查看:20
本文介绍了用重音转换外来字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些文本与数据库中的文本进行比较.在数据库中,当我将数据库文本与我的字符串进行比较时,任何带有重音符号的文本都会像在 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?

推荐答案

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

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');

另一种解决方案是在编码之前将文本转换为 ISO-8859-1,但这可能会破坏信息(ISO-8859-1 包含的字符几乎没有 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));

这篇关于用重音转换外来字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆