【问题标题】:How to display image from RSS feed using PHP如何使用 PHP 显示来自 RSS 提要的图像
【发布时间】:2019-03-06 22:31:50
【问题描述】:

我正在尝试显示来自 rss 提要项的附件的图像。我当前的代码仅使用提要源的 url 作为图像源,因此不显示任何内容。我试图编辑正在寻找图像的行,但没有成功。有人在 php 中看到错误吗?

这是我正在寻找图像的地方...

$has_image = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $image);

这里是显示的地方...

if ($has_image == 1) {
            $description = '<div id="image"><img class="feed-item-image" src="' . $image['src'] . '" /></div>' . $description;
        }

完整的 PHP...

function get_rss_feed_as_html($feed_url, $max_item_cnt = 1, $show_date = true, $show_description = true, $max_words = 0, $cache_timeout = 7200, $cache_prefix = "/tmp/rss2html-")
{
    $result = "";
    // get feeds and parse items
    $rss = new DOMDocument();
    $cache_file = $cache_prefix . md5($feed_url);
    // load from file or load content
    if ($cache_timeout > 0 &&
        is_file($cache_file) &&
        (filemtime($cache_file) + $cache_timeout > time())) {
            $rss->load($cache_file);
    } else {
        $rss->load($feed_url);
        if ($cache_timeout > 0) {
            $rss->save($cache_file);
        }
    }
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'content' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
        $content = $node->getElementsByTagName('encoded'); // <content:encoded>
        if ($content->length > 0) {
            $item['content'] = $content->item(0)->nodeValue;
        }
        array_push($feed, $item);
    }
    // real good count
    if ($max_item_cnt > count($feed)) {
        $max_item_cnt = count($feed);
    }
    $result .= '<ul class="feed-lists">';
    for ($x=0;$x<$max_item_cnt;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $result .= '<li class="feed-item">';
        $result .= '<div class="feed-title"><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></div>';
        if ($show_date) {

            $date = date('l F d, Y', strtotime($feed[$x]['date']));
            $result .= '<small class="feed-date"><em>'.$date.'</em></small>';
        }
        if ($show_description) {
            $description = $feed[$x]['desc'];
            $content = $feed[$x]['content'];
            // find the img
            $found = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
            // no html tags
            $description = strip_tags(preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', "$1$3", $description), '');
            // whether cut by number of words
            if ($max_words > 0) {
                $arr = explode(' ', $description);
                if ($max_words < count($arr)) {
                    $description = '';
                    $w_cnt = 0;
                    foreach($arr as $w) {
                        $description .= $w . ' ';
                        $w_cnt = $w_cnt + 1;
                        if ($w_cnt == $max_words) {
                            break;
                        }
                    }
                    $description .= " ...";
                }
            }
            // add img if it exists


                if ($found)
                {
                echo '<div id="image"><img class="feed-item-image" src="' . (string)$match[1] . '" /></div>';
                }
            $result .= '<div class="feed-description">' . $description;

        }
        $result .= '</li>';
    }
    $result .= '</ul>';
    return $result;
}

function output_rss_feed($feed_url, $max_item_cnt = 1, $show_date = true, $show_description = true, $max_words = 100)
{
    echo get_rss_feed_as_html($feed_url, $max_item_cnt, $show_date, $show_description, $max_words);
}

?>

谢谢

【问题讨论】:

  • 我的回答有帮助吗?
  • 我把你的回答打断了一点,现在正在采购一个空白.gif。
  • 你开始很好,使用适当的 DOM 解析器解析 RSS 提要,但随后你只需将其作为文本转储到数组中并开始使用正则表达式。当您使用 DOM 元素时,为什么不在初始循环中进行此搜索?这对你来说会更容易、更安全。
  • 好电话..我会做一些调整。

标签: php rss


【解决方案1】:

试试这个

$found = preg_match("/img src=\"([^\"]+)\"/", $content, $match);

if ($found)
{
    echo '<div id="image"><img class="feed-item-image" src="' . (string)$match[1] . '" /></div>';
}

您的 css 文件和 feed-item-image 类中可能存在问题。你能试试吗

"&lt;img src=\"" . (string)$match[1] . "\"&gt;";

而不是

'<div id="image"><img class="feed-item-image" src="' . (string)$match[1] . '" /></div>'

【讨论】:

  • 谢谢,但这只是一起跳过了图像元素。
  • @KevMoe 可能在您的 css 文件和 feed-item-image 类中存在问题。你能试试 "";而不是 '
    '
  • 再次感谢...仍然没有运气。我将使用更多代码更新我的问题。
【解决方案2】:

我无法使用内容元素找到图像文件。我把它一块一块地拆开,在物品描述中找到了。不是最好的解决方法,但它现在可以工作。

$found = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $description, $match);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多