简体   繁体   English

将可能无限数量的参数传递给数组,但PHP命令行脚本中的单个数字变量除外

[英]Pass potentially infinite number of arguments to array except a single numeric variable in a PHP command line script

I'm trying to make a PHP script that will take a potentially infinite number of URLs from the command line as arguments. 我正在尝试制作一个PHP脚本,该脚本将使用命令行中潜在的无限数量的URL作为参数。 I also need to pass an argument that only has a single numeric value possible (to specify timeout), eg: 我还需要传递一个只能包含单个数字值的参数(以指定超时),例如:

./urltest.php 60 url1.com url2.com url3.com

I'm not exactly sure how to specify argv[1] to be a single numeric variable while at the same time the rest of the arguments (ie the list of urls) go into an array. 我不确定如何将argv [1]指定为单个数字变量,而同时其余参数(即url列表)进入数组。 Maybe something like: 也许像这样:

$timeout = $argv[1];
$args = func_get_args();    

function numfilter($num) {
    return !is_numeric($num);
}

$urls = array_filters($args, 'numfilter');

?

Thanks in advance! 提前致谢!

I don't think you want func_get_args . 我认为您不需要func_get_args That's for getting all the arguments for the current function. 那是为了获取当前函数的所有参数。 It has nothing to do with the command line. 它与命令行无关。 I'd do it something like 我会像

//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]将是脚本的名称。

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

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