使用PHP重新排序RSS提要


Reordering RSS feed using PHP

我需要过滤和重新排序一个RSS提要。

使用PHP,我如何检测机箱url=""中是否有链接?

大部分将是空的,但我想把这个标签中有链接的整个项目移到顶部。

下面是我将要使用的代码示例。我猜我会用isset ?比如:

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> 

如何使用SimpleXML检查附件URL是否为空:

<?php
$xml = new new SimpleXMLElement($xml_content)
foreach( $xml->item AS $item ) {
    if( !empty( $item->enclosure['url'] ))
        // Do whatever you want
}

根据您想要做的事情,您可以将$item放在一个新的数组中,构建一个新的XML文件,等等

您看过SimpleXML吗?您将能够轻松地在xml对象中反转条目,例如:

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

我觉得你应该看看php dom xml

DOM XML的简单示例:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
  {
  print $item->nodeName . " = " . $item->nodeValue . "<br>";
  }
?> 

你可以阅读完整的教程W3Schools(推荐)