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

时间:2022-10-14 20:56:24

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.

6 个解决方案

#1


13  

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

如果文件编码如下,请务必将编码指定为UTF-8:

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.

htmlspecialchars的默认字符集是ISO-8859-1(从PHP v5.4开始,默认字符集被转换为'UTF-8'),这可以解释为什么当它遇到多字节字符时会出现问题。

#2


5  

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!

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

#3


4  

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.

我刚才有这个问题,旧的行为(我相信PHP 5.2.7之前)是尽管腐败仍然返回字符串,但是从那个版本开始它会抛出这个错误。

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

我的解决方案包括使用// IGNORE修饰符编写脚本以通过iconv提供我的字符串以删除损坏的数据。

(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).

(我们有一个损坏的数据库,它有一些UTF-8中的字符串,一些在latin-1中,通常在列上有错误定义的字符类型)。

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

(看看对Tatu的回答的评论,我将从查看(和玩)$ charset变量的内容开始。

#4


1  

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

正确的代码,以便不会出现任何错误:

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

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.

除此之外,您还可以使用str_replace将一些不良字符替换为您的需要,然后使用htmlentities函数。

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.

看看这个RSS源它取代了更大的html符号到GT;阅读the rss feed时看起来可能不太好的标签。您可以用“ - ”符号或“)”等替换它。

#5


1  

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!

有同样的问题,因为我在utf-8字符串上使用substr。错误很少见,似乎是随机的。只有在多字节字符串上剪切字符串时才会出错!

mb_substr solved the problem :)

mb_substr解决了问题:)

#6


0  

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.

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

#1


13  

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

如果文件编码如下,请务必将编码指定为UTF-8:

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.

htmlspecialchars的默认字符集是ISO-8859-1(从PHP v5.4开始,默认字符集被转换为'UTF-8'),这可以解释为什么当它遇到多字节字符时会出现问题。

#2


5  

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!

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

#3


4  

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.

我刚才有这个问题,旧的行为(我相信PHP 5.2.7之前)是尽管腐败仍然返回字符串,但是从那个版本开始它会抛出这个错误。

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

我的解决方案包括使用// IGNORE修饰符编写脚本以通过iconv提供我的字符串以删除损坏的数据。

(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).

(我们有一个损坏的数据库,它有一些UTF-8中的字符串,一些在latin-1中,通常在列上有错误定义的字符类型)。

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

(看看对Tatu的回答的评论,我将从查看(和玩)$ charset变量的内容开始。

#4


1  

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

正确的代码,以便不会出现任何错误:

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

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.

除此之外,您还可以使用str_replace将一些不良字符替换为您的需要,然后使用htmlentities函数。

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.

看看这个RSS源它取代了更大的html符号到GT;阅读the rss feed时看起来可能不太好的标签。您可以用“ - ”符号或“)”等替换它。

#5


1  

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!

有同样的问题,因为我在utf-8字符串上使用substr。错误很少见,似乎是随机的。只有在多字节字符串上剪切字符串时才会出错!

mb_substr solved the problem :)

mb_substr解决了问题:)

#6


0  

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.

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