Convert foreign characters with accents(用重音转换外来字符)
问题描述
我正在尝试将一些文本与数据库中的文本进行比较.在数据库中,当我将数据库文本与我的字符串进行比较时,任何带有重音符号的文本都会像在 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));
这篇关于用重音转换外来字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用重音转换外来字符
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01