0

I have an RSS feed which has an image and some text. I want to grab only the URL from the image and put it into a variable.

I guess the image is on the description of the feed, how can I do to grab it? I tried getting it with regular expression and it didn't work. How can I do this properly with regular expression so I can only get the src="" part.

<?php
    $feed = simplexml_load_file("the_feed");
    foreach ($feed->channel->item as $item)
    {
        $description = $item->description;
    }
?>

Maybe I should use something like:

$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);
$imageurl['image'] = $matches[1];
echo $imageurl['image'];

1 Answer 1

1

I think you have already answered your question :))) only thing is that the regex you want to use is not so good I guess, better is this $imgpattern = '/src="([^"]+)"/i';

Sign up to request clarification or add additional context in comments.

1 Comment

also, for improvement, you could match the whole img tag like this: '/<img\s+src="([^"]+)"/i'

Your Answer

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