尝试使用 UTF-8 字符串时 PHP htmlspecialchars() 函数出错

发布于 2024-10-03 18:44:20 字数 665 浏览 0 评论 0原文

我做了以下事情:

  1. 我有一个包含数据的电子表格。其中一行中有一个 ü 字符。
  2. 我将其保存为 OpenOffice.org 中的 CSV 文件。当它要求我输入字符编码时,我选择 UTF-8。
  3. 我使用 Navicat 创建一个 MySQL 数据库表、InnoDB,采用 UTF-8 utf8_general 编码并导入 CSV。
  4. 我尝试使用 PHP 函数 htmlspecialchars($string, ENT_COMPAT, 'UTF-8') ,其中 $string 是包含特殊 ü 字符的字符串。

它给了我一个错误:参数中的多字节序列无效。当我将 'UTF-8' 更改为 'ISO8859-1' 时,不会引发错误,但显示不正确的字符。 (“未知字符”字符,看起来像

如果我使用 HTML 表单更新数据库中的字符串,错误就会消失并且字符会正确显示,但是,当然后我在 Navicat 中查看记录,它看起来有两个字符:

[1/4][A 上面有一些东西]

一些多字节不被视为一个字符。`

什么是发生了什么事,哪里出了问题,我该怎么办?

I did the following things:

  1. I have a spreadsheet with data. One of the rows has a ü character in it.
  2. I save this as a CSV file in OpenOffice.org. When it asks me for a character encoding, I choose UTF-8.
  3. I use Navicat to create a MySQL database table, InnoDB with UTF-8 utf8_general encoding and import the CSV.
  4. I try to use PHP function htmlspecialchars($string, ENT_COMPAT, 'UTF-8') where $string is the string containing the special ü character.

It gives me an error: Invalid multibyte sequence in argument. When I change 'UTF-8' with 'ISO8859-1', no error is thrown, but the incorrect character is shown. (The 'unknown character' character, looks like <?>)

If I use an HTML form to update the string in the database, the error disappears and the character is displayed correctly, however, when I then look at the record in Navicat, it looks two characters:

[1/4][A with some thing on top of it]

Some multibyte that isn't seen as one character.`

What is going on, where are things going wrong, and what can I do about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橘虞初梦 2024-10-10 18:44:20

虽然我不明白“无效的多字节”错误从何而来,但我很确定 htmlspecialchars()不是你的罪魁祸首

就该函数而言,字符集 ISO-8859-1、ISO-8859-15、UTF-8、cp866、cp1251、cp1252 和 KOI8-R 实际上是等效的,因为受 htmlspecialchars() 影响的字符在所有这些字符集中占据相同的位置。

根据我的理解,htmlspecialchars() 对于 UTF-8 字符串应该可以正常工作,无需指定字符集。我敢打赌,包含表单的 HTML 页面或您使用的数据库连接都不是 UTF-8 编码的。对于后者,请尝试

SET NAMES utf8;

在执行插入之前向 mySQL 发送 a 。

Although I don't understand where the "invalid multibyte" error comes from, I'm pretty sure htmlspecialchars() is not your culprit:

For the purposes of this function, the charsets ISO-8859-1, ISO-8859-15, UTF-8, cp866, cp1251, cp1252, and KOI8-R are effectively equivalent, as the characters affected by htmlspecialchars() occupy the same positions in all of these charsets.

In my understanding, htmlspecialchars() should work fine for a UTF-8 string without specifying a character set. My bet would be that either the HTML page containing the form, or the database connection you use is not UTF-8 encoded. For the latter, try sending a

SET NAMES utf8;

to mySQL before doing the insert.

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