0
我想爲我的網站創建一個站點地圖 所以基本上我掃描主頁鏈接 並提取鏈接並對提取的鏈接遞歸執行相同的操作使用鏈接:識別外部鏈接和鏈接的完整地址
function get_contents($url = '') {
if($url == '') { $url = $this->base_url; }
$curl = new cURL;
$content = $curl->get($url);
$this->get_links($content);
}
public function get_links($contents){
$DOM = new DOMDocument();
$DOM->loadHTML($contents);
$a = $DOM->getElementsByTagName('a');
foreach($a as $link){
$h = $link->getAttribute('href');
$l = $this->base.'/'.$h;
$this->links[] = $l ;
$this->get_contents($l);
}
}
它工作正常,但有幾個問題
1-
我得到一些鏈接IKE
www.mysite.com/http://www.external.com
我可以這樣做
if(stripos($link , 'http') !== false
||
stripos($link , 'www.') !== false
||
stripos($link , 'https') !== false
)
{
if(stripos($link , 'mysite.com') !== false)
{
//ignor this link (yeah i suck at regex and string mapping)
}
}
,但它似乎很複雜,速度慢,有沒有辦法找出一個鏈接是一個外部鏈接的所有標準和清潔的方式?
2 -
有沒有辦法處理相對路徑? 我得到這樣
www.mysite.com/../Domain/List3.html
一些事情obviusly這是不對的 我可以從鏈接中刪除(../),但它可能無法與所有的鏈接 工作反正是有找到一個鏈接的完整地址?
您使用的是哪種CMS或框架 - 如果有的話 - 可能有更好的方法? –
@jakenoble無! – max