简体   繁体   English

取决于if语句的不同数组值

[英]Different Array Values depending on the if statement

I am building a web crawler which scans links, titles and meta descriptions from links that are found from one url submitted 我正在构建一个网络爬虫,该爬虫将从提交的一个URL中找到的链接扫描链接,标题和元描述

This if statement i think is correct. 我认为这是正确的if陈述。 $description is the variable which holds all the descriptions from the array $link. $ description是用于保存数组$ link中所有描述的变量。 But i notice not all sites have a meta description (wikipedia for example) so i have decided that i would like the first twenty characters to act as the description if the description is empty. 但是我注意到并非所有站点都有一个元描述(例如Wikipedia),因此我决定如果描述为空,我希望前20个字符充当描述。 (By the way, the function and calling of everything works, i just wanted you to see it) (顺便说一句,所有的功能和调用都有效,我只想让您看到它)

     if ($description == '') {
    $html = file_get_contents($link);    
    preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re);
    $res = get_custom_excerpt($re[1]);
    echo "\n";
    echo $res;
    echo "\n";

    }

However, in the array, the links are stored in [link], the title of the link in [title] and the description in [description]. 但是,在数组中,链接存储在[link]中,链接的标题存储在[title]中,描述存储在[description]中。 But i don't know how i would cope with adding $res to my array and to only use if the if statement works. 但是我不知道如何将$ res添加到数组中,并且仅在if语句有效时使用。

$output = Array();

   foreach ($links as $thisLink) {
   $output[] = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink), getMetas($res));
     } 
    print_r($output);

You can use array_push() to add $res back to your array and then evaluate the array however you need to; 您可以使用array_push()将$ res添加回数组,然后根据需要评估该数组; not 100% sure what you're trying to do... 不能100%确定您要做什么...

From your wording I think you want to do this: 从您的措辞来看,我认为您想这样做:

$outputs = array();

foreach ($links as $thisLink) {
    $output = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));

    if ($output['description'] == null) {
        $output['description'] = getMetas($res);
    }

    $outputs[] = $output;
}

You might want to adjust the if statement because I do not know what getMetas() returns when there is not description. 您可能需要调整if语句,因为我不知道没有描述时getMetas()返回什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM