如何正确收集内容,避免使用带有substr_tags的html标签? [英] How to get correctly content and avoid breaking html tags using strip_tags with substr?

查看:228
本文介绍了如何正确收集内容,避免使用带有substr_tags的html标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的页面中,我有一些来自RSS Feed的帖子预览。每个帖子预览显示约300个字符。当用户点击展开按钮时,将替换为 #post 。 #post显示剩余的帖子。

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.

一切都很好,但$ #post 的格式是不好,不可读。所以我想到允许< br>< b>< p> 标签,这将使它可以被阅读。因为我不想让用户分心,我希望在300个字符后允许标签。

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.

使用以下方法,可以打破一些 $ start 结束, $ rest 开始的标签。这意味着没有很好的可读输出。

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;

我的问题是如何保持 $ start $ rest 相同(无标签),直到300个字符,之后 $ 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?

以下是RSS提要结构的示例(来自查看页面源)。

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.

推荐答案

如下所示:

$start = substr($entry->description, 0, 300);
if(($pos = stripos($start, "<")) !== false) {
    $start = strip_tags(substr($start, 0, $pos));
    $rest = substr($entry->description, $pos);
}
else {
    $start = strip_tags($start);
    $rest = substr($entry->description, 300);
}

好的,这只是一个概念。获取前300个字符并检查损坏的标签。如果在此之前破碎,并从这一点获得休息。如果没有破坏只是剥离和休息。至少有一个问题:

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:


  • 你从来没有$开始的长度(strip_tags之后可能没有剩下),可以使用循环长度检查但是eeee ...效率

编辑
好​​的,得到它: / p>

EDIT Ok, get it:

$start = "";
$chars = 400;
while(strlen($start) < 300) { 
    $start = strip_tags(substr($rss, 0, $chars));
    $chars += 50;
}
$pos = stripos($rss, substr($start, strlen($start) - 50));
$rest = substr($rss, $pos+50);

好的,很讨厌,有些情况下它失败了(可重复的文本可能是:D) ,在 Ideone 进行测试

Ok, little nasty and there are some cases on which it fails(with repetable text probably:D), tested on Ideone

这篇关于如何正确收集内容,避免使用带有substr_tags的html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆