【问题标题】:print_r statement doesn't seem to workprint_r 语句似乎不起作用
【发布时间】:2012-12-20 13:49:17
【问题描述】:

我正在构建一个网络爬虫。它找到页面上的所有链接及其标题和元描述等。它做得很好。然后我写了一个数组,它给出了我想要的链接的所有起始 url。因此,如果它抓取一个链接,并且它的 url 以数组中给出起始 url 的任何值开头,则插入到 $news_stories 中。

唯一的问题是它似乎没有插入它们。页面返回空白,现在它说 array_intersect 语句需要一个数组,而我没有指定我拥有的数组。

总之,我很难理解我的代码在哪里不起作用以及为什么没有插入所需的网址。

$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/'
);

// BBC Algorithm
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);
    }
}

$new_stories = array();

foreach ($output as $new_array) {
    if (array_intersect($output['link'], $bbc_values) == true) {
        $news_stories[] = $new_array;
    }

    print_r($news_stories);
}

【问题讨论】:

  • 尝试使用var_dump 而不是print_r,它会提供更多有用的信息
  • "print_r doesn't work" 是一个荒谬的假设。相信语言,并寻找实际的错误..

标签: php


【解决方案1】:

您将数组标记为 $new_stories 并打印 $news_stories..... diff is 'S'

检查代码是否进入这个循环,我认为不是......

if (array_intersect($output['link'], $bbc_values) == true) {
    echo 'here';
}

【讨论】:

    【解决方案2】:

    嗯,我不认为 array_intersect 是你比较需要的http://php.net/manual/en/function.array-intersect.php

    也许你想寻找 in_array http://php.net/manual/en/function.in-array.php

    【讨论】:

      【解决方案3】:

      使用返回参数时,此函数使用内部输出缓冲,因此不能在 ob_start() 回调函数中使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 2020-06-23
        • 1970-01-01
        • 2013-07-23
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多