0

我将 1-10 个参数传递给一个函数,然后我希望该函数为每个参数自行运行,但返回以前的数据和新数据。

所以我有一个如下功能:

function scrape_google_result_source($link,$link2) //$link is "test" $link2 is "test2"
    {
        $html  = $link;
        $cache = $html; //this is my first return

        $html  = $link2;
        $cache = $cache . $html; //this is my first and second return

        return $cache; //now I am returning it so it will be "testtest2"
    }

如果我手动传入 $link1 和 $link2 然后对其进行编码以与它们一起使用,则此方法有效,我希望它为传入的每个参数自行运行,然后设置“$cache .= new result”,然后我将返回结果所有的争论都过去了。

可悲的是,我没有除此之外的代码,因为我不确定从哪里开始,我确实找到了func_num_args();可能工作的 php 函数?非常感谢任何帮助。

谢谢,西蒙

4

2 回答 2

1

就我个人而言,我发现解析数组和循环更容易:

function scrape_google_result_source($links)
{

    $cache = '';
    if( !is_array( $links ) )
    {
        return 'not array';
    }
    foreach( $links as $key=>$link )
    {
        $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
        $url       = $link;
        $ch        = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        $html  = curl_exec($ch);
        $cache .= $html;
        curl_close($ch);


    }
    return $cache; //now I am returning it
}


$links_array = array( 'http..','http...');
$html = scrape_google_result_source( $links_array );
于 2013-10-08T12:40:31.130 回答
1

尝试这个;

function scrape_google_result_source($link,$link2)
    {
        $numargs = func_num_args();
        foreach($numargs as $n){
            $link =  func_get_arg($n);
            $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
            $url       = $link;
            $ch        = curl_init();
            curl_setopt($ch, CURLOPT_TIMEOUT, 100);
            $html   = curl_exec($ch);
            $cache .= $html; //this is my first return
            curl_close($ch);

        }

        return $cache; //now I am returning it
    }

func_get_arg 手册

于 2013-10-08T12:40:40.160 回答