KevMoe
KevMoe

Reputation: 443

How to display image from RSS feed using PHP

I am trying to display an image sourced from the enclosure of an rss feed item. My current code is using just the url of the feed source as the image source thereby not displaying anything. I have attempted to edit the line that is looking for the image but I have been unsuccessful. Does anyone see an error in the php?

Here is where I am looking for the image...

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

Here is where it is displayed...

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

Full 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);
}

?>

Thank you

Upvotes: 1

Views: 1601

Answers (2)

KevMoe
KevMoe

Reputation: 443

I was unable to find the image file using the content element. I broke it down piece by piece and found it in the item description. Not the best way to resolve but it'll work for now.

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

Upvotes: 0

Marat Badykov
Marat Badykov

Reputation: 844

Try this

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

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

Maybe problem in your css file and feed-item-image class. Can you just try

"<img src=\"" . (string)$match[1] . "\">";

instead of

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

Upvotes: 2

Related Questions