2012-07-20 59 views
0

位卡在此, 我在做的是循環一個包含指向我的站點的鏈接的URL列表, 在找捕捉用於產生鏈接和可替換地存儲被用作鏈接錨文本的HTML代碼,如何從html源中找到URL中的關鍵字,但將鏈接和錨文本存儲在數組中

[代碼由瑪蒂除去見下文]

因此用於martylinks代碼使用一個函數即時通訊仍然試圖建立,這是即時通訊有一點麻煩,但對於你們我確信它非常簡單..

這是我find_marty_links功能

function find_marty_links($file, $keyword){ 
    //1: Find link to my site <a href="http://www.***martin***-gardner.co.uk" target="_blank" title="Web Developer">Web Developer</a> 
    //2: copy the FULL HTML LINK to array 
    //3: copy the REL value? NOFOLLOW : FOLLOW to array 
    //4 copy TITLE (if any) to array 
    //5 copy Anchor Text to array 

    $htmlDoc = new DomDocument(); 
    $htmlDoc->loadhtml($file); 

    $output_array = array(); 
    foreach($htmlDoc->getElementsByTagName('a') as $link) { 

      // STEP 1 
     // SEARCH ENTIRE PAGE FOR KEYWORD? 
      // FIND A LINK WITH MY KEYWORD? 
      preg_match_all('???', $link, $output); //???// 

      if(strpos($output) == $keyword){ 


       // STEP 2 
       // COPY THE FULL HTML FOR THAT LINK? 
       $full_html_link = preg_match(??); 
       $output_array['link_html'] = $full_html_link; 

       // STEP 3 
       // COPY THE REL VALUE TO ARRAY 
       $link_rel = $link->getAttribute('rel'); 
       $output_array['link_rel'] = $link_rel; 

       // STEP 4 
       // COPY TITLE TO ARRAY 
       $link_title = $link->getAttribute('title'); 
       $output_array['link_title'] = $link_title; 

       // STEP 5 
       // COPY ANCHOR TEXT TO ARRAY 
       $anchor_exp = expode('>'); //??? 
       $anchor_txt = $anchor_exp[2];//?? 
       $output_array['link_anchor'] = $anchor_txt; 

      } 

    } 
} 

!! UPDATE! 需要生產

$results = array('link_html' => '<a title="test" href="http://site.com" rel="nofollow">anchor text</a>', 
       'link_rel' => 'nofollow', 
       'link_title' => 'test', 
       'link_anchor' => 'anchor text' 
       ) 

感謝您的幫助小夥子們像下面的Array ..

中號

回答

1

確定這裏是更新後的代碼:

function find_marty_links($file, $keyword){ 
    $htmlDoc = new DomDocument(); 
    $htmlDoc->loadhtml($file); 
    $links = array(); 

    foreach($htmlDoc->getElementsByTagName('a') as $link) { 
     $url = $link->getAttribute('href'); 
     $title = $link->getAttribute('title'); 
     $text = $link->nodeValue; 
     $rel = $link->getAttribute('rel'); 

     if(strpos($url,$keyword) !== false || strpos($title,$keyword) !== false || strpos($text,$keyword) !== false) 
     { 
      $links[] = array('url' => $url, 'text' => $text, 'title' => $title, 'rel' => $rel); 
     } 
    } 

    return $links; 
} 
+0

喜@Pitchinnate感謝我已更新了我的問題,並提供了關於我在尋找什麼的簡要輸出, – Marty 2012-07-20 13:44:28

+0

在您的函數中,您正在運行一個鏈接循環,然後檢查t o看看關鍵字是否在頁面上的任何地方,而不只是在鏈接中。爲什麼?你關心關鍵字是否在其他地方,但在鏈接? – Pitchinnate 2012-07-20 14:08:30

+0

foreach鏈接檢查我的關鍵字是否在該鏈接中,並將該鏈接的詳細信息存儲在數組中?更新的代碼添加$鏈接NOT $文件 – Marty 2012-07-20 14:12:51

相關問題