【问题标题】:How can I parse RSS description tag with PHP如何使用 PHP 解析 RSS 描述标签
【发布时间】:2014-02-03 18:08:48
【问题描述】:

我正在寻找一种简单的方法来解析 RSS 描述标签,以便存储 img 链接以及显示的任何文本。

我正在使用 Last RSS 来解析 RSS 提要,但是,每个提要的描述标签都有很多不同的信息,而且它们都有很大的不同。

目前我正在使用 Simple HTML Dom Parser 从描述中获取 img 链接,但是,这似乎不适用于所有提要。例如,它无法识别来自 dribbble 的以下描述中的 img:

<![CDATA[<a height="150" href="http://dribbble.com/shots/1405514-sparrows" width="200"><img alt="sparrows" height="600" src="http://d13yacurqjgara.cloudfront.net/users/14152/screenshots/1405514/sparrows.jpg" width="800" /></a><p>We are currently working on identity for a new Canadian project called "sparrows". A place for creatives to gather some useful readings.</p>]]>

获取此信息的最佳方式是什么?

【问题讨论】:

    标签: php rss simple-html-dom


    【解决方案1】:

    最好的方法可能是使用 xpath,但假设你已经决定使用简单的 html dom:

    require('simple_html_dom.php');
    
    $rss =<<<EOF
    <description>
    <![CDATA[<a height="150" href="http://dribbble.com/shots/1405514-sparrows" width="200"><img alt="sparrows" height="600" src="http://d13yacurqjgara.cloudfront.net/users/14152/screenshots/1405514/sparrows.jpg" width="800" /></a><p>We are currently working on identity for a new Canadian project called "sparrows". A place for creatives to gather some useful readings.</p>]]>
    </description>
    EOF;
    
    $rss_dom = str_get_html($rss);
    
    # find the cdata string from the description and strip out the CDATA part
    $cdata = $rss_dom->find('description', 0)->text();
    $str = preg_replace('/<!\[CDATA\[(.*)\]\]>/', '\1', $cdata);
    
    # make a new dom with just the cdata html and query that
    $html_dom = str_get_html($str);
    echo $html_dom->find('img', 0)->src;
    
    # http://d13yacurqjgara.cloudfront.net/users/14152/screenshots/1405514/sparrows.jpg
    

    【讨论】:

      【解决方案2】:

      我使用 LastRSS 内置函数来去除 CDATA 标记并保持其余的 html 属性完好无损。

      这很简单,如下:

      // Create lastRSS object
      $rss = new lastRSS;
      
      $rss->CDATA = 'content';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多