note 71587 modified in function.array-search by bobby
| From: | bobby@php.net | Date: | Mon, 11 Dec 2006 19:58:17 +0000 |
| Subject: | note 71587 modified in function.array-search by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120513@lists.php.net to get a copy of this message | ||
You can use array_search to simply parse a CLI program's input parameters such as:
[user@server ~]$ ./someCLIprog.php --username user1 --password 123password --option1 whatever
You can easily parse this information using the following code snippets:
<?php
// Get the username
$userkey = array_search("--username", $argv);
// Make sure both that the token was found and followed by another valid token, AKA not one that
begins with a -
if ($userkey && !empty($argv[$userkey+1]) && strcmp($argv[$userkey+1]{0},
'-')) {
$username = $argv[$userkey+1];
}
// If you want the token to be required add the following else statement
else {
exit("No username provided\n");
}
// repeat for each optional parameter,
?>
--was--
You can use array_search to simply parse a CLI program's input parameters such as:
[user@server ~]$ ./someCLIprog.php --username user1 --password 123password --option1 whatever
You can easily parse this information using the following code snippets:
<?php
// Get the username
$userkey = array_search("--username", $argv);
// Make sure both that the token was found and followed by another valid token, AKA not one that
begins with a -
if ($userkey && !empty($argv[$userkey+1]) && strcmp($argv[$userkey+1]{0} !=
'-')) {
$username = $argv[$userkey+1];
}
// If you want the token to be required add the following else statement
else {
exit("No username provided\n");
}
// repeat for each optional parameter,
?>
http://php.net/manual/en/function.array-search.php