Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php substr()奇怪的输出。这是什么原因造成的?_Php_Html Entities - Fatal编程技术网

Php substr()奇怪的输出。这是什么原因造成的?

Php substr()奇怪的输出。这是什么原因造成的?,php,html-entities,Php,Html Entities,所以我使用substr来限制我显示的新闻文章的数量。代码如下: substr(strip_tags($news['content']),0,$content_length) . '...'; 这个问题只会在晴天霹雳中发生。。当它试图在撇号附近截断时。我得到以下输出Hoye&#…在本例中,$news['content']是“Hoye的药店将关闭…”。本例中的$content\u length值恰好为8。有人建议尝试 MbSypBase,但这并没有解决这个问题。看起来内容包含编码的HTML字符实体

所以我使用substr来限制我显示的新闻文章的数量。代码如下:

substr(strip_tags($news['content']),0,$content_length) . '...';

这个问题只会在晴天霹雳中发生。。当它试图在撇号附近截断时。我得到以下输出
Hoye&#…
在本例中,
$news['content']
“Hoye的药店将关闭…”
。本例中的
$content\u length
值恰好为8。有人建议尝试<代码> MbSypBase<代码>,但这并没有解决这个问题。

看起来内容包含编码的HTML字符实体,而子串恰好在其中一个中间砍字符串。

e、 g


如果您在浏览器中查看完整字符串,它会将编码的实体转换为其“显示”版本,这样您就可以看到真实的字符,但一旦用substr将其切分一半,您将得到部分&#xxx部分,因为它无法转换。

您如何确认该字符串是
Hoye的
?在我看来,它是
Hoye;s
(或类似的东西)。请检查原始输出,而不是通过浏览器过滤的输出。是的,我想这可能就是正在发生的情况。。hmm解决方案?请参阅:建议的修复方法:在位置后寻找下一个空格并在那里切掉。您可以解码字符串(
html\u entity\u decode()
),这样里面就只有“real”字符,然后在上面使用
mb\u substr()
,这样就不会像
substr()
那样拆分unicode字符。但这可能会进一步破坏代码,使其预期的编码字符不再存在。尝试了mb_substr(strip_标记(html_entity_decode($news['content'])),0,$content_length)。。不抽雪茄:(
$string = "Hello &amp; Goodbye";
$broken =  substr($string, 0,7); // Hello &a