<?php
function getPageLink($url)
{
set_time_limit(0);
$html = file_get_contents($url);
preg_match_all("/<a(s*[^>]+s*)href=([\"|']?)([^\"'>\s]+)([\"|']?)/ies",$html,$out);
$arrLink = $out[3];
$arrUrl = parse_url($url);
if( isset($arrUrl['path']) && !empty($arrUrl['path']) )
{
$dir = str_replace('\\','/',$dir = dirname($arrUrl['path']));
if( $dir == '/') $dir = '';
}
if(is_array($arrLink) && count($arrLink) > 0)
{
$arrLink = array_unique($arrLink);
foreach($arrLink as $key=>$val)
{
$val = strtolower($val);
if( preg_match('/^#*$/isU',$val))
{
unset($arrLink[$key]);
}
else if(preg_match('/^\//isU',$val))
{
$arrLink[$key] = 'http://'.$arrUrl['host'].$val;
}
else if(preg_match('/^javascript/isU',$val))
{
unset($arrLink[$key]);
}
else if(preg_match('/^mailto:/isU',$val))
{
unset($arrLink[$key]);
}
else if( !preg_match('/^\//isU',$val) && strpos($val,'http://') === FALSE)
{
$arrLink[$key] = 'http://'.$arrUrl['host'].$path.'/'.$val;
}
}
}
sort($arrLink);
return $arrLink;
}
print_r(getPageLink('http://www.hao123.com'));
?>
正则提取URL
最新推荐文章于 2024-04-01 04:40:20 发布