Reputation: 105
I need to filter and reorder an RSS
feed.
Using PHP
, how would I detect if there is a link in the enclosure url=""
?
Most of these will be empty, but I'd like to move the whole item that has a link in this tag to the top.
Here is an example of the code I'll be working with. I'm guessing I would use isset
? Something like:
if (isset($enclosure)){
HOW WOULD I SELECT THE PARENT ITEM? AND EVERYTHING IN IT? AND THEN MOVE IT TO THE TOP?
}
<item>
<title><![CDATA[Article title 1]]></title>
<link><![CDATA[http://www.articlelinkone.com]]></link>
<pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
<description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
<enclosure url="" type="image/jpeg"></enclosure>
<thumbnail url="" type="image/jpeg"></thumbnail>
<summary><![CDATA[Summary for article]]></summary>
<guid><![CDATA[Guide for article]]></guid>
<source url="http://www.article.com/"/>
</item>
<item>
<title><![CDATA[Article title 1]]></title>
<link><![CDATA[http://www.articlelinkone.com]]></link>
<pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
<description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
<enclosure url="" type="image/jpeg"></enclosure>
<thumbnail url="" type="image/jpeg"></thumbnail>
<summary><![CDATA[Summary for article]]></summary>
<guid><![CDATA[Guide for article]]></guid>
<source url="http://www.article.com/"/>
</item>
<item>
<title><![CDATA[Article title 1]]></title>
<link><![CDATA[http://www.articlelinkone.com]]></link>
<pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
<description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
<enclosure url="http://articleone.com/image1.jpg" type="image/jpeg"></enclosure>
<thumbnail url="http://articleone.com/thumb/image1.jpg" type="image/jpeg"></thumbnail>
<summary><![CDATA[Summary for article]]></summary>
<guid><![CDATA[Guide for article]]></guid>
<source url="http://www.article.com/"/>
</item>
<item>
<title><![CDATA[Article title 1]]></title>
<link><![CDATA[http://www.articlelinkone.com]]></link>
<pubDate><![CDATA[Tue, 26 Feb 2013 15:45:00 EDT]]></pubDate>
<description><![CDATA[Article discription 1<br />Some HTML will exist]]></description>
<enclosure url="" type="image/jpeg"></enclosure>
<thumbnail url="" type="image/jpeg"></thumbnail>
<summary><![CDATA[Summary for article]]></summary>
<guid><![CDATA[Guide for article]]></guid>
<source url="http://www.article.com/"/>
</item>
Upvotes: 1
Views: 248
Reputation: 3171
I think you really should look at php dom xml
Simple Example of DOM XML:
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
You may read the full tutorial on W3Schools (recommended)
Upvotes: 1
Reputation: 3807
Here is how you can check if the enclosure URL is not empty with SimpleXML :
<?php
$xml = new new SimpleXMLElement($xml_content)
foreach( $xml->item AS $item ) {
if( !empty( $item->enclosure['url'] ))
// Do whatever you want
}
Depending on what you want to do, you can put the $item
in a new array, build a new XML file, etc.
Upvotes: 3