1

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

  • Check this out: http://stackoverflow.com/questions/2571232/parse-html-with-phps-html-domdocument –  Jan 05 '14 at 02:58

1 Answers1

2

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
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Mine ended up printing only from the root directory on so: file.exe. I had to prepend the http://domain.com to the output. – tatorface Jan 20 '15 at 15:57