Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Last active May 8, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpk/90123640cb26f2f586f8baaf4439cd8f to your computer and use it in GitHub Desktop.
Save aaronpk/90123640cb26f2f586f8baaf4439cd8f to your computer and use it in GitHub Desktop.
<?php
$html = '<html><body><a href="http://example.com">example</a></body></html>';
$lookfor = 'http://example.com';
$doc = new DOMDocument();
libxml_use_internal_errors(true); # suppress parse errors and warnings
$body = mb_convert_encoding($html, 'HTML-ENTITIES', mb_detect_encoding($html));
@$doc->loadHTML($body, LIBXML_NOWARNING|LIBXML_NOERROR);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
foreach($xpath->query('//a[@href]') as $href) {
$url = $href->getAttribute('href');
if($url == $lookfor) {
// FOUND THE LINK!
echo "Found the link\n";
break;
}
}
@matthiasott
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment