user3134994
user3134994

Reputation: 25

Get href value by id from html string

I have a html string

<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. thank for help

Upvotes: 1

Views: 2113

Answers (1)

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
}

Upvotes: 2

Related Questions