donglvhe7591 2015-03-04 19:12
浏览 46
已采纳

如何从PHP中的tumlbr rss源获取第一张图像

0Here is the relevant part of my rss feed:

    <channel>
        <description></description>
        <title>Untitled</title>
        <generator>Tumblr (3.0; @xxx)</generator>
        <link>http://xxx.tumblr.com/</link> 
        <item>
            <title>Title</title>
            <description>&lt;figure&gt;&lt;img src="https://31.media.tumblr.com/c78c7t3abd23423549d3bb0f705/tumblr_inline_nkp9z234d0uj.jpg"/&gt;&lt;/figure&gt;</description>
            <link>http://xxx.tumblr.com/post/99569244093</link>
            <guid>http://xxx.tumblr.com/post/99569244093</guid>
            <pubDate>Thu, 09 Oct 2014 11:19:33 -0400</pubDate>
        </item>
    </channel>

Using the answer from other questions on here I tried this:

$content = file_get_contents("http://xxx.tumblr.com/rss"); 
$feed = new SimpleXmlElement($content); 
$imgs = $feed->channel->item[0]->description->xpath('//img');
    foreach($imgs as $image) {
            echo (string)$image['src'];     
};

This is returning an empty array for $imgs

Does it have something to do with the tags being &lt; &gt; etc?

and if so what can I do?

  • 写回答

3条回答 默认 最新

  • duanfu3884 2015-03-04 20:28
    关注

    You can get it from the description, which seems to include a HTML image tag for the image, by using a simple regular expression with preg_match:

    $content = file_get_contents("http://xxx.tumblr.com/rss");
    $feed    = new SimpleXmlElement($content);
    $img     = (string)$feed->channel->item[0]->description;
    
    if (preg_match('/src="(.*?)"/', $img, $matches)) {
        $src = $matches[1];
        echo "src = $src", PHP_EOL;
    }
    

    Output:

    src = http://40.media.tumblr.com/58d24c3009638514325b113859ba369f/tumblr_nk0mwfhKXU1sl87kjo1_500.jpg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧