【问题标题】:Recursion with array's PHP [closed]使用数组的 PHP 递归 [关闭]
【发布时间】:2020-02-20 11:44:32
【问题描述】:

我有一个函数 (listarUrls ()),它返回/扫描它在网页上找到的所有 url。 我需要对于函数返回给我的每个 url,我返回列表/扫描该页面的所有 url 根据用户的要求多次,即

        .If the user asks for 1 iteration of the url www.a.com, bring back:
            -$arry[0] www.1.com
            -$arry[1] www.2.com
            -..... So with all the urls you find in www.a.com

        .If the user asks for 2 iteration of the url www.a.com, bring back:
            -$arry[0] www.1.com
                -$arry[0][0] www.1-1.com
                -$arry[0][1] www.1-2.com
                -...So with all the urls you find in www.1.com
            -$arry[1] www.2.com
                -$arry[1][0] www.2-1.com
                -$arry[1][1] www.2-2.com
                -...So with all the urls you find in www.2.com
            -...

        .If the user asks for 3 iteration of the url www.a.com, bring back:
            -$arry[0] www.1.com
                -$arry[0][0] www.1-1.com
                    -$arry[0][0][0] www.1-1-1.com
                    -$arry[0][0][1] www.1-1-2.com
                    -...So with all the urls you find in www.1-1.com
                -$arry[0][1] www.1-2.com
                    -$arry[0][1][0] www.1-2-1.com
                    -$arry[0][1][1] www.1-2-2.com
                    -...So with all the urls you find in www.1-2.com
            -$arry[1] www.2.com
                -$arry[1][0] www.2-1.com
                    -$arry[1][0][0] www.2-1-1.com
                    -$arry[1][0][1] www.2-1-2.com
                    -...So with all the urls you find in www.2-1.com
                -$arry[1][1] www.2-2.com
                    -$arry[1][1][0] www.2-2-1.com
                    -$arry[1][1][1] www.2-2-2.com
                    -...So with all the urls you find in www.2-2.com
        -...

有人能解释一下这个主题吗?

【问题讨论】:

  • 你卡在哪里了?
  • 我不知道如何根据用户请求的迭代次数使用每个网站的 url 递归地创建数组。
  • 您不必递归地创建数组。您必须递归迭代并返回结果。
  • 你能给我一个我必须创建的函数的例子吗?

标签: php arrays recursion


【解决方案1】:

这是网络抓取,带有指示调查深度的选项。

我们可以有如下的函数定义:

function scrapeURLs($url,$steps,&$visited_urls = []);

这里,$url 是我们正在抓取的当前 URL。 $steps 是我们正在调查的步骤。如果$steps == 1 在我们的递归函数中的任何位置,我们将停止进一步抓取。 $visited_urls 是为了确保我们不会两次访问同一个 URL 进行抓取。

片段:

<?php
ini_set('max_execution_time','500');
libxml_use_internal_errors(true); // not recommended but fine for debugging. Make sure HTML of the URL follows DOMDocument requirements
function scrapeURLs($url,$steps,&$visited_urls = []){
    $result = [];   
    if(preg_match('/^http(s)?:\/\/.+/',$url) === 0){ // if not a proper URL, we stop here, but will have to double check if it's a relative URL and do some modifications to current script
        return $result;
    }

    $dom = new DOMDocument();
    $dom->loadHTMLFile($url);

    // get all script tags
    foreach($dom->getElementsByTagName('script') as $script_tag){
        $script_url = $script_tag->getAttribute('src');
        if(!isset($visited_urls[$script_url])){
            $visited_urls[$script_url] = true;
            $result[$script_url] = $steps === 1 ? [] : scrapeURLs($script_url,$steps - 1,$visited_urls);    // stop or recurse further  
        }       

    }   

    // get all anchor tags
    foreach($dom->getElementsByTagName('a') as $anchor_tag){
        $anchor_url = $anchor_tag->getAttribute('href');
        if(!isset($visited_urls[$anchor_url])){
            $visited_urls[$anchor_url] = true;
            $result[$anchor_url] = $steps === 1 ? [] : scrapeURLs($anchor_url,$steps - 1,$visited_urls);
            // stop or recurse further
        }
    }

    /* Likewise, you can capture several other URLs like CSS stylesheets, image URLs etc*/

    return $result;
}

print_r(scrapeURLs('http://yoursite.com/',2));

【讨论】:

  • 完美,非常感谢 vivek_23
  • @In0cybe 很高兴为您提供帮助 :) 我希望您按照这些步骤操作,这将为您提供不同深度的正确结果。
【解决方案2】:

array_walk_recursive — 递归地将用户函数应用于数组的每个成员 https://www.php.net/manual/en/function.array-walk-recursive.php

【讨论】:

  • 感谢 K IV,但我怎样才能创建一个函数来根据用户的要求创建具有所有递归级别的数组?
猜你喜欢
  • 1970-01-01
  • 2020-01-22
  • 2012-12-06
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 2015-02-19
相关资源
最近更新 更多