是否有任何正则表达式来获取alt属性?
示例:<img src="http://static.adzerk.net/Advertisers/a61df59863ed44e59446d0b9bb805c56.jpg" title="" alt="blahblah" border="0" height="90" width="728">
我想从上面的图片标签中获取“blahblah”。
答案 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');