简体   繁体   English

通过HTML字符串按ID获取href值

[英]Get href value by id from html string

I have a html string 我有一个html字符串

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

With php dom I want get http://tomtuoi.com/file.exe url by id. 使用php dom我想通过id获取http://tomtuoi.com/file.exe网址。 thank for help 谢谢你的帮助

Do like this.. 这样吧

<?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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM