mkrisch
mkrisch

Reputation: 105

Reordering RSS feed using PHP

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

Answers (3)

Oliver M Grech
Oliver M Grech

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

Arthur
Arthur

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

Malachi
Malachi

Reputation: 33700

Have you looked into SimpleXML? You'll be able to easily reverse the items within the xml object with something like:

$str = file_get_contents($url);
$xml = simplexml_load_string($str);
$items = array_reverse($xml->xpath('item'));

Upvotes: 2

Related Questions