获取alt属性的正则表达式

时间:2014-09-19 02:10:57

标签: php html regex image

是否有任何正则表达式来获取alt属性? 示例:<img src="http://static.adzerk.net/Advertisers/a61df59863ed44e59446d0b9bb805c56.jpg" title="" alt="blahblah" border="0" height="90" width="728">

我想从上面的图片标签中获取“blahblah”。

1 个答案:

答案 0 :(得分:4)

Don't use regexes for parsing HTML

未经测试 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');