0

The code below gets rss feeds from yahoo and it works but I have a little problem, when I rub the code, it returns the feeds but it also gives me an error like -

Undefined offset: 1 in /Applications/MAMP/htdocs/feedbackFtms/rss.php on line 8

line 8 is - $foundImg = $findImg[1];

<div class="feed">
    <link rel="stylesheet" href="css/profilepage.css">

    <?php
        function getImageFromContext($context){

        $findImg = explode('<img src="',$context);
        $foundImg = $findImg[1];
        $findExt = explode('.jpg',$foundImg);
        $getIMG = $findExt[0].'.jpg';
        return $getIMG;
        }

       function getFeed($url){
        $x = simplexml_load_file($url);
        echo "<ul>";
        foreach($x->channel->item as $entry) {
            echo "<li><img src='".getImageFromContext($entry->description)."' /><a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
            }
            echo "</ul>";
        }
        getFeed("http://news.yahoo.com/rss/entertainment");
    ?>
    </div>
user2486495
  • 1,709
  • 11
  • 19
Marvel Gerrard
  • 89
  • 1
  • 3
  • 9

2 Answers2

0

Try this,

 foreach($x->channel->item as $entry) {
        echo "<li><img src='".getImageFromContext('<img src="'.$entry->description)."' /><a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
        }
        echo "</ul>";
    }

I am not sure if you need the last two lines before the return in the getImageFromContext function.

Works On Mine
  • 1,111
  • 1
  • 8
  • 20
0

Some articles don't have image.

you can change a little bit your code as is and I guess it will work but for this more convenient to use regular expressions.

<?php
function getImageFromContext($context){

    $findImg = explode('<img src="',$context);
    if(isset($findImg[1])){
        $foundImg = $findImg[1];
        $findExt = explode('.jpg',$foundImg);
        $getIMG = '<img src="' . $findExt[0].'.jpg' . '" />';

        return $getIMG;
    }

    return '';
    }

function getFeed($url){
    $x = simplexml_load_file($url);

    echo "<ul>";
    foreach($x->channel->item as $entry) {
        echo "<li>".getImageFromContext($entry->description)."<a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
        }
        echo "</ul>";
    }
    getFeed("http://news.yahoo.com/rss/entertainment");

?>

</div>