php htmlspecialchars(): 参数中的多字节序列无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
htmlspecialchars(): Invalid multibyte sequence in argument
提问by gautamlakum
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.
谢谢。
采纳答案by Tatu Ulmanen
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”),这可以解释为什么当遇到多字节字符时事情会变得混乱。
回答by gingerCodeNinja
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
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),当显示错误关闭时会显示此错误!
回答by berty
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 iconvusing 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 变量的内容。
回答by Sailab Rahi
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 feedit 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; 阅读您的 RSS 提要时可能不好看的标签。您可以将其替换为“-”符号或“)”等。
回答by CoR
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!
有同样的问题,因为我substr
在 utf-8 字符串上使用。
错误很少发生,而且似乎是随机的。仅当字符串在多字节字符上被剪切时才会发生错误!
mb_substr
solved the problem :)
mb_substr
解决了问题:)
回答by mark
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。