从html字符串中获取id的href值

时间:2014-01-05 02:57:06

标签: php dom

我有一个html字符串

<html>
<head></head>
<body>
bla bla bla <br />
<a id="downloadbutton" href ="http://tomtuoi.com/file.exe";
</body>
</html>

使用php dom我希望通过id获取http://tomtuoi.com/file.exe url。感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这样做..

<?php
$html='<html>
<head></head>
<body>
bla bla bla <br />
<a id="downloadbutton" href ="http://tomtuoi.com/file.exe";
</body>
</html>';
$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $tag) {
    echo $tag->getAttribute('href'); //"prints" http://tomtuoi.com/file.exe
}