handy little bit of code I wrote to take arguments from the command line and parse them for use in my apps. <?php $i = implode(" ",$argv); //implode all the settings sent via clie $e = explode("-",$i); // no lets explode it using our defined seperator '-' //now lets parse the array and return the parameter name and its setting // since the input is being sent by the user via the command line //we will use stristr since we don't care about case sensitivity and //will convert them as needed later. while (list($index,$value) = each($e)){ //lets grap the parameter name first using a double reverse string // to get the begining of the string in the array then reverse it again // to set it back. we will also "trim" off the "=" sign $param = rtrim(strrev(stristr(strrev($value),'=')),"="); //now lets get what the parameter is set to. // again "trimming" off the = sign $setting = ltrim(stristr($value,'='),"="); // now do something with our results. // let's just echo them out so we can see that everything is working echo "Array index is ".$index." and value is ".$value."\r\n"; echo "Parameter is ".$param." and is set to ".$setting."\r\n\r\n"; } ?> when run from the CLI this script returns the following. [root@fedora4 ~]# php a.php -val1=one -val2=two -val3=three Array index is 0 and value is a.php Parameter is and is set to Array index is 1 and value is val1=one Parameter is val1 and is set to one Array index is 2 and value is val2=two Parameter is val2 and is set to two Array index is 3 and value is val3=three Parameter is val3 and is set to three
Recent Comments