substr() 奇怪的输出。是什么原因造成的?
所以我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来内容包含编码的 HTML 字符实体,并且您的子字符串恰好在其中之一的中间截断了字符串。
例如,
如果您在浏览器中查看完整的字符串,它会将编码实体转换为其“显示”版本,因此您将看到真实的字符,但是一旦您用子字符串将其切成两半,您将得到部分字符而是使用 &#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.
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.