search for in the  
<getmyuidgetrusage>
Last updated: Thu, 19 May 2005

getopt

(PHP 4 >= 4.3.0, PHP 5)

getopt -- Gets options from the command line argument list

Description

array getopt ( string options [, array longopts] )

Returns an associative array of option / argument pairs based on the options format specified in options, or FALSE on an error.

On platforms that have the C function getopt_long, long options can be specified with the parameter longopts (as of PHP 4.3.0).

<?php
// parse the command line ($GLOBALS['argv'])
$options = getopt("f:hp:");
?>

The options parameter may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. For example, an option string x recognizes an option -x, and an option string x: recognizes an option and argument -x argument. It does not matter if an argument has leading white space.

This function will return an array of option / argument pairs. If an option does not have an argument, the value will be set to FALSE.

Note: This function is not implemented on Windows platforms.



User Contributed Notes
getopt
dante at wiw dot org
30-Jan-2005 10:44
i didn't like the way getopt worked, exactly, so i wrote a new variant, that other people would possibly like to see. (works more like perl's function)

it reads an array of options like:

$opAr = array ("-a|--append","-l|--list","-i|--input:");

$op = bgetop($opAr);

and parses the command line, returning an array like

$op['cmdline'] = $param;  ...

it has a small bug that can easily be avoided ... haven't yet determined how to work around the particular case where the bug exists, but otherwise is very robust.

it also accepts wildwards as the last option, or the output from another program like 'find' :

./getop.php `find /etc -name *.conf`

populates the $op array with the filenames returned from find.  it's pretty nifty.

the source is at : http://higginsforpresident.net/projects/source/getopt-0.1.phps

i didn't want to post it here until i fixed that one condition, but the function works nicely (as expected) if you don't use duplicate beginnings for different option array strings:

$opAr = array("-f:","--foo");

bgetopt() sees --foo needs no 'next input', but -f exists in '--foo', so ./getop.php -f foobar set's f to 'true', which is the
expected result of array("-f","--foo") or array("-f|--foo"), but not ("-f:","--foo");

enjoy.  (my first submission here ... be kind.)
grange
15-Oct-2004 09:50
getopt() will return an empty array if you call it more than once.
vedanta at maintec dot com
12-May-2004 04:36
A sample use  :

#!/usr/bin/php
<?php
$opt
= getopt("s:f:r:u:");
$help="***Error :url_mailer.php -s <subject> -f <sender_email> -r <receipient_email> -u <url_to_mail>";
if(
$opt[s]=='' || $opt[r]=='' || $opt[u]=='' || $opt[f]=='' ){ echo "$help\n";exit(); }

# prepare the mail message from the url
$url=trim($opt[u]);
$message=file_get_contents($url);

# prepare mail headers
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: $opt[f] \r\n";

# send the mail
mail($opt[r],$opt[s],$message,$headers);
?>
chris at tiny dot net
23-Apr-2004 09:17
"phpnotes at kipu dot co dot uk" and "tim at digicol dot de" are both wrong or misleading.  Sean was correct.  Quoted space-containing strings on the command line are one argument.  It has to do with how the shell handles the command line, more than PHP.  PHP's getopt() is modeled on and probably built upon the Unix/POSIX/C library getopt(3) which treats strings as strings, and does not break them apart on white space.

Here's proof:

$ cat opt.php
#! /usr/local/bin/php
<?php
$options
= getopt("f:");
print_r($options);
?>
$ opt.php -f a b c
Array
(
   [f] => a
)
$ opt.php -f 'a b c'
Array
(
   [f] => a b c
)
$ opt.php -f "a b c"
Array
(
   [f] => a b c
)
$ opt.php -f a\ b\ c
Array
(
   [f] => a b c
)
$
klewan at chello dot at
19-Dec-2003 10:00
For all buddies outside who dont get getopt to work :)
here is a php variant with just standard function calls

<?
//Set Defaults
$options["a"]="valA";
$options["b"]="valB";

parseArgs($argv,&$options);

//options will be $options["FLAG"] (if not argument is given like -h the $options["h"] will be set to bolean true

if($options["h"] == true) {
      
print_usage();
       exit(
1);
}
var_dump($options);

function
parseArgs($a = array(), $r) {
$f=NULL;
       for(
$x = 0; $x < count($a); $x++) {

               if(
$a[$x]{0} == "-") {
                      
$f=$a[$x];
                      
$r[substr($f,1,strlen($f))]=true;
               }
               if (
$f != NULL) {
                       if ((
$a[$x+1] != NULL) && ($a[$x+1] != "") && ($a[$x+1] != "") && ($a[$x+1]{0} != "-")) {
                              
$r[substr($f,1,strlen($f))]=$a[$x+1];
                       } else {
                          
$f=$a[x+1];
                       }
               }
       }
}

function
print_usage() {
       echo
"-a bla bla\n";
       echo
"-b bla bla\n";
       echo
"-h display this help\n";

}
?>
carl at thep dot lu dot se dot nospam
17-Jul-2003 09:07
Unlike POSIX.2 getopt(), this function does not modify argv, so it's
only useful if you have no non-option arguments. More often than
not, you're probably better off examining $argv yourself.

<getmyuidgetrusage>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT