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);
}
4

3 回答 3

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 */
  }
}
于 2012-12-20T19:06:11.227 回答
0

好的,我不完全理解这里的代码。但我认为 $output 数组应该在第一个 foreach 循环之外声明,并且每个数组都应该附加到它上面?因为从您正在编写的代码中,只有最后一个 $link 的详细信息将存储在 $output 中

另外,这里的 $data 是什么?你用它做什么?

于 2012-12-20T19:04:14.730 回答
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);
于 2012-12-20T19:07:31.600 回答