note 58533 added to function.stristr

From: Date: Mon, 07 Nov 2005 17:17:50 +0000
Subject: note 58533 added to function.stristr
Groups: php.notes 
Request: Send a blank email to php-notes+get-98201@lists.php.net to get a copy of this message
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 [root@fedora4 ~]# ---- Manual Page -- http://www.php.net/manual/en/function.stristr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58533 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58533&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58533&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98201) next »