Jonathan
Jonathan

Reputation: 683

Getting Image Source from RSS CDATA

I currently have this function to grab the image src field from CDATA in an RSS feed.

function echo_url($string) {
  preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);
  return $match[0][0];
}

Only bad thing is that it's picking up the first URL that appears in the CDATA instead of the image src itself, is there anyway I can access or return just the image src instead of the first URL?

Here's an example of the CDATA I get.

![CDATA[Duration: 357 sec<br>URL: http://www.test.com/videos/999682/test-test-video<br><img src="http://test.com/images/test.jpg"><br><iframe src="http://embeds.test.com/embed/999682" frameborder=0 width=650 height=518 scrolling=no></iframe>]]

All I'm after in getting is the 'img src'.

Any help would be brilliant (bit of a beginner at php)

Thanks

Upvotes: 1

Views: 1565

Answers (1)

Tony
Tony

Reputation: 124

If you simply want to return the image source, then this function will work:

/**
 * @param string $cdata
 * @return string|FALSE
 */
function getImgSrc($cdata)
{
    $found = preg_match("/img src=\"([^\"]+)\"/", $cdata, $match);
    if ($found)
    {
        return $match[1];
    }
    else
    {
        return FALSE;
    }
}

Upvotes: 1

Related Questions