如何使用带有 substr 的 strip_tags 获取正确的内容并避免破坏 html 标签?
在我的页面中,我有一些来自 RSS 源的帖子预览。每个帖子预览显示约 300 个字符。当用户单击展开按钮时,#post-preview
将替换为 #post
。 #post 显示帖子的其余部分。
一切都很好,但是 #post
的格式不好,不可读。所以我想到允许
标签,这样就可以读取了。因为我不想让用户分心,所以我希望在 300 个字符之后允许使用标签。
使用以下方法,可以破坏 $start
结束和 $rest
开始的一些标签。这意味着没有良好的可读输出。
$start = strip_tags(substr($entry->description, 0, 300));
$rest = strip_tags(substr($entry->description, 300), '<b><p><br>');
$start . $rest;
我的问题是如何在 300 个字符之前保持 $start
和 $rest
相同(无标签),之后 $rest
将显示格式化的帖子?还有其他方法可以做到这一点吗?
以下是 RSS 提要结构的示例(来自查看页面源代码)。
<item><guid isPermaLink="false"></guid><pubDate></pubDate><atom:updated></atom:updated><category domain=""></category><title></title><description></description><link></link><author></author></item>
我正在寻找一种不会影响性能的方法。
In my page I have some post previews from RSS feeds. Every post preview shows about 300 characters. When a user clicks on expanding button, then the #post-preview
is replaced with the #post
. The #post shows the rest of the post.
Everything fine with this but the format of the #post
is not good, not readable. So I thought of allowing <br><b><p>
tags, it will make it ok to be read. Because I don't want the user to be distracted, I want the tags to be allowed after the 300 chars.
With the following method, it is possible to break some tags where the $start
ends and $rest
starts. This means no good readable output.
$start = strip_tags(substr($entry->description, 0, 300));
$rest = strip_tags(substr($entry->description, 300), '<b><p><br>');
$start . $rest;
My question is how can I keep $start
and $rest
the same (no tags) until the 300 char, and after that $rest
will show the formatted post? Are there any other ways of doing this?
Here is an example of a RSS feed structure (from view page source).
<item><guid isPermaLink="false"></guid><pubDate></pubDate><atom:updated></atom:updated><category domain=""></category><title></title><description></description><link></link><author></author></item>
I am looking for a way that does not kill performance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
比如:
好吧,这只是一个概念。获取前 300 个字符并检查损坏的标签。如果损坏,请在其之前切掉并从此时开始休息。如果没有破裂,就脱掉衣服并休息。至少有一个问题:
编辑
好吧,明白了:
好吧,有点令人讨厌,在某些情况下它会失败(可能有可重复的文本:D),在 上测试创意
Something like:
Ok, it's just a concept. Gets first 300 chars and checks for broken tag. If broken cut before it and get $rest from this point. If not broken just strip and get rest. There is at least 1 problem:
EDIT
Ok, get it:
Ok, little nasty and there are some cases on which it fails(with repetable text probably:D), tested on Ideone