[英]PHP XML RSS Assistance
I'm reading a podcast RSS feed from wordpress (one of my shows) - and it works great - but, In the RSS feed's XML there's a bunch of other stuff that I don't want in the "description" like this stuff (this is exactly how it looks in the feed - it's divs and javascript for facebook and twitter things that aren't necessary for what I'm doing): 我正在阅读wordpress播客的RSS提要(我的一个节目)-效果很好-但是,在RSS提要的XML中,还有很多我不希望在“描述”中出现的东西,例如:这就是Feed中的样子-它是divs和javascript,用于处理Facebook和Twitter,而这些对于我正在做的事情不是必需的):
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
How can I tell it NOT to pull this information in the description? 我如何告诉它不要在描述中提取此信息?
This is my general code that I found online: 这是我在网上找到的一般代码:
<?php
$rss = new DOMDocument();
$rss->load('http://mywordpresssite.com/rss/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
Like I said, the code works - but I really need to get rid of all that excess text and junk that follows in the description. 就像我说的那样,代码可以正常工作-但我确实需要摆脱描述中所有多余的文本和垃圾。
Any thoughts would be helpful. 任何想法都会有所帮助。
Ok, never mind - I figured it out. 好吧,没关系-我想通了。
I used this: 我用这个:
$text = substr($description, 0, strpos( $description, '<'));
And it will take everything after the &alt;
&alt;
之后的所有内容 and get rid of it. 并摆脱它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.