3

The script I am using to pull in the feed titles is:

<?php  

function getFeed($feed_url) {  

$content = file_get_contents($feed_url);  
$x = new SimpleXmlElement($content);  

echo "<ul>";  

foreach($x->channel->item as $entry) {  
    echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>    </li>";
    $i++;
    if($i==5) break;        
}  
echo "</ul>";  
}  
?> 

I would like to pull in the images for each title/article and place them next to the title.

The float part is easy. I'm having trouble getting the actual image in. I've tried that mark like this: <img src="$entry->image" />, but it didn't work.

How would I go about this please?

3
  • an <img> with float: left? or what do you exactly want? Commented Oct 10, 2013 at 16:34
  • so... echo "<img src="$entry->whatever" />"? Commented Oct 10, 2013 at 16:39
  • Yeah an image with float eft, but the float part is easy - I'm having trouble getting the actual image in. I've tried that mark like this: <img src="$entry->image" /> it didn't work. Commented Oct 10, 2013 at 18:08

2 Answers 2

2

As the answer to random's suggestion:

  • I'm using $item['description'] to represent the description string.
  • Then match for img tag
  • Output image tag
  • Then remove the image tag from the original $content and output that, you're left with just the description text

    <?php 
        $content = $item['description'];
        preg_match('/(<img[^>]+>)/i', $content, $matches);
        echo $matches[0];
        echo str_replace($matches[0],"",$content);
    ?>
    
Sign up to request clarification or add additional context in comments.

Comments

1

When displaying the feed contents, variables such as $entry->link and $entry->title work because they are valid, present and required elements in a standard RSS feed item.

To call $entry->image would require the source RSS to use and populate that optional element.

Some feeds may include that data, but most will not.

Alternatively, you can write your own function or method to scan the contents of the description element and find any suitable image to include as you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.