我试图做一个PHP脚本,将从命令行将可能无限数量的URL作为参数。 我还需要传递只有一个数值的参数(指定超时),例如:
./urltest.php 60 url1.com url2.com url3.com
我不确定如何指定argv [1]是一个单一的数字variables,同时其余的参数(即url列表)进入一个数组。 也许是这样的:
$timeout = $argv[1]; $args = func_get_args(); function numfilter($num) { return !is_numeric($num); } $urls = array_filters($args, 'numfilter');
?
提前致谢!
我不认为你想要func_get_args
。 这是为了获得当前函数的所有参数。 它与命令行无关。 我会这样做
//i don't like altering the global $argv $urls = $argv; //take off the first element of the array, leaving only urls $script = array_shift($argv); //shift off script name, per other post :) $timeout = array_shift($urls); if(!is_numeric($timeout)) exit ("First argument was not a number") //array_shift always makes me break out perl style, so this however you want //your $urls variable now contains an array with arguments 2 - n
确保对array_shift进行两次调用,因为argv [0]将是脚本的名称。