页:
[1]
| sunjian998 | 2008-1-25 10:09 AM |
|
获取指定页面中的所有链接
[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]);
}elseif(preg_match('/^\//isU',$val)){
$arrLink[$key] = 'http://'.$arrUrl['host'].$val;
}elseif(preg_match('/^javascript/isU',$val)){
unset($arrLink[$key]);
}elseif(preg_match('/^mailto:/isU',$val)){
unset($arrLink[$key]);
}elseif( !preg_match('/^\//isU',$val) && strpos($val,'http://') === FALSE){
$arrLink[$key] = 'http://'.$arrUrl['host'].$path.'/'.$val;
}
}
}
sort($arrLink);
return $arrLink;
}
$links = getPageLink('http://www.360quan.com');
echo "<pre>";
print_r($links);
echo "</pre>";
[/php]
[[i] 本帖最后由 sunjian998 于 2008-1-25 06:13 PM 编辑 [/i]] |
| wish2000 | 2008-1-29 03:34 AM |
|
运行有错误,我修正了正则的/符号。
[code]
<?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]);
}elseif(preg_match('/^\//isU',$val)){
$arrLink[$key] = 'http://'.$arrUrl['host'].$val;
}elseif(preg_match('/^javascript/isU',$val)){
unset($arrLink[$key]);
}elseif(preg_match('/^mailto:/isU',$val)){
unset($arrLink[$key]);
}elseif( !preg_match('/^\//isU',$val) && strpos($val,'http://') === FALSE){
$arrLink[$key] = 'http://'.$arrUrl['host'].$path.'/'.$val;
}
}
}
sort($arrLink);
return $arrLink;
}
$links = getPageLink('http://www.360quan.com');
echo "<pre>";
print_r($links);
echo "</pre>";
?>[/code] |
| sunjian998 | 2008-1-29 08:56 AM |
|
获取指定页面中的所有链接
代码出错了,我把完整的放在附件中.
[[i] 本帖最后由 sunjian998 于 2008-1-29 04:58 PM 编辑 [/i]] |
| firstsmileman | 2008-4-25 01:19 AM |
|
| my core is so liltle |
Powered by Discuz! Archiver 6.1.0
© 2001-2006 Comsenz Inc.
Processed in 0.00794 second(s), 2 queries |