views:

56

answers:

2

I'm trying to make a PHP script that will take a potentially infinite number of URLs from the command line as arguments. I also need to pass an argument that only has a single numeric value possible (to specify timeout), e.g.:

./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 (i.e. the list of urls) go into an array. 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!

+2  A: 

I don't think you want 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
Alan Storm
Thank you, this helped a lot!
tehalive
+1  A: 

Be sure to make two calls to array_shift, since argv[0] will be the name of the script.

mfabish