2012-12-20 46 views
0

開頭我有一個值的數組。如果數組的值以

我的爬蟲掃描網頁並插入所有鏈接,鏈接的標題和描述是一個多維數組。

但現在我有一個新的數組,我只想鏈接,說明和標題等,如果他們有任何價值開始在陣列中($ bbc_values)

但我真的不知道該怎麼辦這個。在實際的代碼方面,我已經得到了很多,但任何人都可以給我任何想法a)爲什麼我的代碼不工作b)對於我的問題的建議?

$bbc_values = array('http://www.bbc.co.uk/news/health-', 'http://www.bbc.co.uk/news/politics-', 'http://www.bbc.co.uk/news/uk-', 'http://www.bbc.co.uk/news/technology-', 'http://www.bbc.co.uk/news/england-', 'http://www.bbc.co.uk/news/northern_ireland-', 'http://www.bbc.co.uk/news/scotland-', 'http://www.bbc.co.uk/news/wales-', 'http://www.bbc.co.uk/news/business-', 'http://www.bbc.co.uk/news/education-', 'http://www.bbc.co.uk/news/science_and_enviroment-', 'http://www.bbc.co.uk/news/entertainment_and_arts-', 'http://edition.cnn.com/'); 


foreach ($links as $link) { 
    $output = array(
     "title"  => Titles($link), //dont know what Titles is, variable or string? 
     "description" => getMetas($link), 
     "keywords" => getKeywords($link), 
     "link"  => $link     
    ); 

    if (empty($output["description"])) { 
     $output["description"] = getWord($link); 
    } 
} 
$data = implode(" , ", $output['link']); 
foreach ($output as $new_array) { 
    if (in_array($output, $bbc_values)) { 
    $news_stories[] = $new_array; 
} 

var_dump($news_stories); 
} 

回答

0

好的,我並沒有完全理解這裏的代碼。 但我認爲$ output數組應該在第一個foreach循環之外聲明,並且每個數組都應該被追加到它之後? 因爲根據你寫的代碼,只有最後$鏈接的細節將被存儲在$輸出中

另外,什麼是$數據在這裏?你用它做什麼?

+0

請在下次使用「添加評論」來討論您的疑惑。答案只能用於提供......好的答案。 –

+1

他不會在他的rep –

0

打開$bbc_values成正則表達式:

$bbc_re = '/^('.implode('|', array_map('quotemeta', $bbc_values)).')/'; 

然後用這個表達式過濾鏈接。

foreach ($links as $link) { 
    if (preg_match($bbc_re, $link)) { 
    /* Do stuff with $link */ 
    } 
} 
+0

有添加評論選項。將嘗試它! – hek2mgl

0

我假設你想要的東西是有鏈接,與在bbc_values的聯繫和另外一個字符串$data用逗號分隔的所有鏈接的列表開始的數組。試試這個:

<?php 

$bbc_values = array('http://www.bbc.co.uk/news/health-', 'http://www.bbc.co.uk/news/politics-', 'http://www.bbc.co.uk/news/uk-', 'http://www.bbc.co.uk/news/technology-', 'http://www.bbc.co.uk/news/england-', 'http://www.bbc.co.uk/news/northern_ireland-', 'http://www.bbc.co.uk/news/scotland-', 'http://www.bbc.co.uk/news/wales-', 'http://www.bbc.co.uk/news/business-', 'http://www.bbc.co.uk/news/education-', 'http://www.bbc.co.uk/news/science_and_enviroment-', 'http://www.bbc.co.uk/news/entertainment_and_arts-', 'http://edition.cnn.com/'); 

$news_stories = array(); 
$all_links = array(); 
$news_links = array(); 

foreach ($links as $link) { 
    $item = array(
     "title"  => Titles($link), 
     "description" => getMetas($link), 
     "keywords" => getKeywords($link), 
     "link"  => $link     
    ); 

    if (empty($item["description"])) { 
     $item["description"] = getWord($link); 
    } 


    foreach($bbc_values as $bbc_value) { 
     // note the '===' . this is important 
     if(strpos($item['link'], $bbc_value) === 0) { 
      $news_stories []= $item; 
      $news_links []=$item['link']; 
      break; 
     } 
    } 

    $all_links[] = $item['link']; 
} 

$data_all_links = implode(' , ', $all_links); 
$data_news_links = implode(' , ', $news_links); 
var_dump($news_stories); 
+0

他說他想要鏈接_begin with_元素的$ bbc_values,而不是完全匹配。 – Barmar

+0

好的,將更新 – hek2mgl

+0

您修正了他代碼中的所有錯誤 - 在適當的位置添加我的答案,我想我們會有一個完整的解決方案。 – Barmar

相關問題