note 59550 deleted from function.getopt by philip

From: Date: Thu, 07 May 2009 22:33:02 +0000
Subject: note 59550 deleted from function.getopt by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154333@lists.php.net to get a copy of this message
Note Submitter: andrew dot waite at databanx dot net ---- After having problems with the getopt() function I developed this to handle parameters in a smilar way to 'nix shell script. Hope this is of some us to someone. #!/usr/bin/php4 <?php # # Purpose: Parse parameters passed to script in way comaparable to shell script # Usage: include_once("/some/path/getArgs.php"); OR # require_once("/some/path/getArgs.php"); depending on context of parent script # Post: Generate an associative array $params # # Author: A.Waite # Date: 09/12/05 # Version: V1.0 $i = 1; $params = array(); for ($i=1; $i < $argc; $i++){ if ( isParam($argv[$i]) ){ if (strlen($argv[$i]) == 1){ //do nothing // single - }elseif ( strlen($argv[$i]) == 2){ //seperated (ie -H www.google.com) or no value parameter (ie. -h) $paramName = $argv[$i]; $paramVal = ( !isParam( $argv[ $i + 1 ] ) ) ? $argv[$i + 1] : null; }elseif ( strlen($argv[$i]) > 2){ //joined parameters ie -Hwww.google.com $paramName = substr($argv[$i],0,2); $paramVal = substr($argv[$i],2); } $params[ $paramName ] = $paramVal; } } #determines whether string is a parameter function isParam($string){ return ($string{0} == "-") ? 1: 0; } ?>

« previous php.notes (#154333) next »