-3

Is there any regex to get the alt attribute? Example: <img src="http://static.adzerk.net/Advertisers/a61df59863ed44e59446d0b9bb805c56.jpg" title="" alt="blahblah" border="0" height="90" width="728">

I want to get the "blahblah" from above image tag.

Michael
  • 89
  • 1
  • 1
  • 8

1 Answers1

4

Don't use regexes for parsing HTML

Untested Demo

$string = '<img src="http://static.adzerk.net/Advertisers/a61df59863ed44e59446d0b9bb805c56.jpg" title="" alt="blahblah" border="0" height="90" width="728">';
$dom = new DOMDocument();
@$dom->loadHTML($string);
$anchor = $dom->getElementsByTagName('img')->item(0);
echo $anchor->getAttribute('alt');
Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496