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

发布于 2024-12-13 21:13:24 字数 375 浏览 2 评论 0原文

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

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

这个问题只发生在蓝月中......当它试图在撇号附近切断时。我得到以下输出 Hoye&#... 在这种情况下,$news['content']“Hoye's Pharmacy will be close... ..”。本例中的 $content_length 值恰好是 8。有人建议尝试 mb_substr 但这并没有解决问题。

So I am using substr to limit how much of a news article I show. Here is the code:

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

The problem happens only in a blue moon.. when it tries to cut off near an apostrophe. I get the following output Hoye&#... In this case, the $news['content'] is "Hoye's Pharmacy will be closed.....". The $content_length value in this case happens to be 8. Someone suggested trying mb_substr but that did not fix the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-12-20 21:13:24

看起来内容包含编码的 HTML 字符实体,并且您的子字符串恰好在其中之一的中间截断了字符串。

例如,

$string = "Hello & Goodbye";
$broken =  substr($string, 0,7); // Hello &a

如果您在浏览器中查看完整的字符串,它会将编码实体转换为其“显示”版本,因此您将看到真实的字符,但是一旦您用子字符串将其切成两半,您将得到部分字符而是使用 &#xxx 部分,因为它无法翻译。

Looks like the content contains encoded HTML character entities, and your substring happens to chop the string in the middle of one of them.

e.g.

$string = "Hello & Goodbye";
$broken =  substr($string, 0,7); // Hello &a

if you view the full string in a browser, it'll translate the encoded entity to its "display" version, so you'll see the real character, but once you chop it half with the substr, you'll get the partial &#xxx portion instead, since it can't be translated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文