2012-12-20 16 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

相关问题