note 49601 deleted from function.preg-match-all by colder
| From: | colder@php.net | Date: | Mon, 16 Apr 2007 16:55:16 +0000 |
| Subject: | note 49601 deleted from function.preg-match-all by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125456@lists.php.net to get a copy of this message | ||
Note Submitter: mpbweb at mbourque dot com
----
Here is a handy function I wrote that will check for broken links on the supplied url.
function dead_links($url) {
// mixed link_checker( $url )
// Returns:
// FALSE if no broken links are found.
// ARRAY containing broken links if any are found.
ob_start();
if( !readfile($url) ) return FALSE;
$body = ob_get_contents();
ob_end_clean();
$pathparts = pathinfo($url);
$urlpattern = "/<a[^>]+href=\"([^\"]+)/i";
preg_match_all($urlpattern,$body,$matches);
foreach( $matches[1] as $link) {
if( strpos($link,"http://") === FALSE ) { // Deal with
relative paths
$link = $pathparts['dirname'] . "/" . $link;
}
$fp = @fopen("$link", "r");
fclose($fp);
if (!$fp) {
$linkArray[] = $link;
}
}
return (is_array($linkArray) ) ? $linkArray : FALSE;
}
Regards,
Michael Bourque