我正在尝试创建一个PHP脚本,该脚本将从命令行获取可能无限数量的URL作为参数。我还需要传递一个只有一个可能的数值的参数(以指定超时),例如:
./urltest.php 60 url1.com url2.com url3.com我不太确定如何将argv1指定为单个数值变量,同时将其余参数(即urls列表)放入一个数组中。可能是这样的:
$timeout = $argv[1];
$args = func_get_args();
function numfilter($num) {
return !is_numeric($num);
}
$urls = array_filters($args, 'numfilter');提前感谢!
发布于 2010-04-07 10:55:10
我不认为你想要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发布于 2010-04-07 10:56:41
确保对array_shift进行两次调用,因为argv将是脚本的名称。
https://stackoverflow.com/questions/2589684
复制相似问题