1

我需要过滤和重新排序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> 
4

3 回答 3

3

以下是使用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 文件等。

于 2013-02-27T15:03:29.230 回答
2

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

$str = file_get_contents($url);
$xml = simplexml_load_string($str);
$items = array_reverse($xml->xpath('item'));
于 2013-02-27T14:49:57.320 回答
1

我认为你真的应该看看 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 的完整教程(推荐)

于 2013-02-27T14:47:59.947 回答