htmlspecialchars():参数中的多字节序列无效

发布于 2024-09-25 12:52:35 字数 207 浏览 6 评论 0原文

我在本地网站上收到此错误。

Warning (2): htmlspecialchars(): Invalid multibyte sequence in argument in [/var/www/html/cake/basics.php, line 207]

有谁知道,问题是什么或者应该采取什么解决方案?

谢谢。

I am getting this error in my local site.

Warning (2): htmlspecialchars(): Invalid multibyte sequence in argument in [/var/www/html/cake/basics.php, line 207]

Does anyone knows, what is the problem or what should be the solution for this?

Thanks.

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

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

发布评论

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

评论(6

孤单情人 2024-10-02 12:52:35

如果您的文件是这样编码的,请务必将编码指定为 UTF-8:

htmlspecialchars($str, ENT_COMPAT, 'UTF-8');

htmlspecialchars 的默认字符集是 ISO-8859-1(从 PHP v5.4 开始,默认字符集已变为 ' UTF-8'),这也许可以解释为什么当遇到多字节字符时事情会变得混乱。

Be sure to specify the encoding to UTF-8 if your files are encoded as such:

htmlspecialchars($str, ENT_COMPAT, 'UTF-8');

The default charset for htmlspecialchars is ISO-8859-1 (as of PHP v5.4 the default charset was turned to 'UTF-8'), which might explain why things go haywire when it meets multibyte characters.

总以为 2024-10-02 12:52:35

我在生产中遇到了这个错误,并发现了一篇关于它的很棒的文章 -

http://insomanic。 me.uk/post/191397106/php-htmlspecialchars-htmlentities-invalid

这似乎是 PHP 中的一个错误(至少对于 CentOS),当显示错误关闭时显示此错误!

I ran in to this error on production and found this great post about it -

http://insomanic.me.uk/post/191397106/php-htmlspecialchars-htmlentities-invalid

It appears to be a bug in PHP (for CentOS at least) that displays this error on when display errors is Off!

海夕 2024-10-02 12:52:35

您正在将损坏的字符数据输入到函数中,或者未指定正确的编码。

我不久前遇到了这个问题,旧的行为(我相信在 PHP 5.2.7 之前)是尽管损坏但仍返回字符串,但从该版本开始它会抛出此错误。

我的解决方案涉及编写一个脚本,通过 iconv 提供字符串,使用 //IGNORE 修饰符删除损坏的数据。

(我们有一个损坏的数据库,其中一些字符串采用 UTF-8 格式,一些字符串采用 latin-1 格式,通常列上定义的字符类型不正确)。

(查看 Tatu 答案的评论,我首先查看(并使用) $charset 变量的内容。

You are feeding corrupted character data into the function, or not specifying the right encoding.

I had this issue a while ago, old behavior (prior to PHP 5.2.7 I believe) was to return the string despite corruption, but since that version it will throw this error instead.

My solution involved writing a script to feed my strings through iconv using the //IGNORE modifier to remove corrupted data.

(We had a corrupted database which had some strings in UTF-8, some in latin-1 usually with incorrectly defined character types on the columns).

(Looking at the comment to Tatu's answer, I would start by looking at (and playing with) the contents of the $charset variable.

影子的影子 2024-10-02 12:52:35

为了不出现任何错误,正确的代码是:

htmlentities($string, ENT_IGNORE, 'UTF-8') ;

除此之外,您还可以使用 str_replace 来替换一些坏字符满足您的需要,然后使用 htmlentities 函数。

看看这个rss feed,它将更大的html符号替换为gt;阅读 RSS 提要时,该标签可能看起来不太好。您可以将其替换为“-”符号或“)”等。

The correct code in order not to get any error is:

htmlentities($string, ENT_IGNORE, 'UTF-8') ;

Beside this you can also use str_replace to replace some bad characters to your needs and then use htmlentities function.

Have a look at this rss feed it replaced the greater html sign to gt; tag which might not look nice when reading thee rss feed. You can replace this with something like "-" sign or ")" and etc.

☆獨立☆ 2024-10-02 12:52:35

遇到同样的问题,因为我在 utf-8 字符串上使用 substr
错误并不常见,而且看起来是随机的。仅当在多字节字符上剪切字符串时才会发生错误!

mb_substr 解决了问题:)

Had the same problem because I was using substr on utf-8 string.
Error was infrequent and seemingly random. Error occurred only if string was cut on multibyte char!

mb_substr solved the problem :)

吹泡泡o 2024-10-02 12:52:35

这实际上是我最常犯的错误之一。

有时我不使用 __() 翻译 - 只是包含 äöü 的纯德语文本。
在那里,注意文件的编码尤其重要。

因此,请确保将包含特殊字符的文件正确保存为 UTF8。

That's actually one of the most frequent errors I get.

Sometimes I dont use __() translation - just plain German text containing äöü.
There it is especially important to mind the encoding of the files.

So make sure you properly save the files that contain special chars as UTF8.

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