Stay hungry, Stay foolish

0%

htmlspecialchars返回空字符串问题

问题描述

一个UTF-8编辑的字符串,截取一定长度后,直接htmpspecialchars($string),显示没有问题:

Miss rabbit teeth.ˉ兔子牙小姐。�…,

但是用htmlspecialchars($string, ENT_QUOTES, ‘UTF-8’),就成一个空的字符串了。

问题分析

首先用mb_detect_encoding()查看了下字符串的编码是UTF-8没错。

而问题跟htmlspecialchars的参数有关,就先从htmlspecialchars,搜索了一下,发现php.net上有对这个问题的描述:

htmlspecialchars/htmlentities returns an empty string when the input
contains an invalid unicode sequence.

字符串在截取后的“�”无法识别,才会返回一个空的字符串。

重新改写了一下截取函数。问题消除。

php.net对这个bug的链接:https://bugs.php.net/bug.php?id=43896

据说打赏我的人,代码没有BUG