Last active
May 8, 2016 13:54
-
-
Save aaronpk/90123640cb26f2f586f8baaf4439cd8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!