正则提取URL

<?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'));
?>

posted on 2012-03-06 19:36  奋斗者  阅读(2858)  评论(0编辑  收藏  举报

导航