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));
这篇关于用重音转换外来字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用重音转换外来字符


基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01