关于Invalid multibyte sequence in argument in check_plain() 的报错

逆流の鱼, 17 四月, 2014

在drupal的使用过程中,某些情景会固定出现这个错误:

Warning: htmlspecialchars(): Invalid multibyte sequence in argument in check_plain() (line 1156 of /drupal_root/includes/bootstrap.inc).

而1156行的代码是这样的:

return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');

造成这个报错的原因多数是因为提交的$text是汉字等双字节文本,而且被substr()这类函数将双字节从中间截断。

这属于php的一个bug,https://bugs.php.net/bug.php?id=47494

对于双字节文本,用mb_substr()替代substr()是可以避免这样的情况。

在drupal的views模块里对title的截取,也出现了这个问题,例如:

菜单: 标签:最新�...

 

而神奇的是,只有当php.ini里的display_errors = Off时,htmlspecialchars才会报错。

我试着将display_errors设为On,重启php后就不再有这类报错了。

 

参考文章:http://insomanic.me.uk/post/191397106/php-htmlspecialchars-htmlentities-invalid

评论