Search code examples
phprssfeed

Php rss show just image in $description part


There is a method that removes the image...

How can i just use the image shown by src="..." in the $description part and remove the description text?

<?php
       $html = "";
       $url = "http://newsfeed.zeit.de/index";
       $xml = simplexml_load_file($url);
       for($i = 0; $i < 1; $i++){
           $description = $xml->channel->item[$i]->description;

           //removes image
           $description = preg_replace("/<img[^>]+\>/i", "", $description);

           $html .= "$description";
       }
       echo $html;
?>

Solution

  • You'd first alter your regex slightly to match the images, then append this to your $html variables instead of the $description like so:

    $description = "Venenatis Adipiscing Aenean Mollis <img src='http://www.domain.com/images/cat.gif' /> Justo Egestas Nibh";
    preg_match("/(<img[^>]+\>)/i", $description, $matches);
    if (isset($matches[0])) {
        $html .= $matches[0];
    } else {
        $html .= $description
    }