-2

I need to extract just image link to use as background image of a div using

<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>

PHP. I can't handle regular expressions to extract the image url. Please help me.

<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>
Kevin
  • 41,694
  • 12
  • 53
  • 70

3 Answers3

1

Try this:

$input $ = '<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';
$regexp = "<img[^']*?src=\"([^']*?)\"[^']*?>"; 
if(preg_match_all("/$regexp/siU", $input, $matches)) {
   var_dump($matches[1]);
}
Ahmed Ziani
  • 1,206
  • 3
  • 14
  • 26
1

A very crude solution... of sorts.

    $s='<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';

    preg_match('@"(http://.*)"@',$s,$m);
    echo '<pre>';
    echo str_replace(array('"','>'),'',$m[1]);
    echo '</pre>';
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
1

A good practice is to use a DOM parser instead of regular expressions when extracting information from HTML.

One possible solution:

  1. Read the HTML into a SimpleXMLElement object
  2. Run an xpath query to find all img tags
  3. Get the src attribute value of the first img tag found

Code:

$html = '<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';

// Long version
$dom        = new SimpleXMLElement($html);
$images     = $dom->xpath('//img');
$firstImage = $images[0];
$src        = $firstImage['src'];

// Short version
$src = (new SimpleXMLElement($html))->xpath('//img')[0]['src'];

$src will then contain (for both versions):

http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png
Community
  • 1
  • 1
mhall
  • 3,671
  • 3
  • 23
  • 35