Last active
August 29, 2015 14:13
-
-
Save franz-josef-kaiser/b4651103168a56dbd4ed to your computer and use it in GitHub Desktop.
Fetch remote file, get all Links using PHPs \DOMDocument
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 | |
// @author Jack (Original Author) incl. error handling | |
// @link http://codegolf.stackexchange.com/a/44340/11940 | |
// modify state | |
$libxml_previous_state = libxml_use_internal_errors( true ); | |
$source = 'http://stroustrup.com/C++.html'; | |
$dom = new \DOMDocument; | |
// Dump error suppression not needed here as LibXML errors are handled above | |
// @$dom->loadHTMLFile( $source ); | |
$dom->loadHTMLFile( $source ); | |
$anchors = $dom->getElementsByTagName('a'); | |
foreach ( $anchors as $a ) { | |
if ( in_array( parse_url( $url = $a->getAttribute('href'), PHP_URL_SCHEME ), [ 'http', 'https', ], true ) ) { | |
echo "{$url}<br>"; | |
} | |
} | |
// handle errors | |
libxml_clear_errors(); | |
// restore errors to mess up global error handling | |
libxml_use_internal_errors( $libxml_previous_state ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now that this is no longer in the context of code golf, please read this answer as well :)