/ Published in: PHP
As seen in [maid450](http://snipplr.com/view/6677/)'s profile, added the ability to parse strings, and the ability to parse quoted strings, also the ability to regognize second arguments after "--" flags.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function parse_args($args) { $last_arg = null; $args = str_getcsv($args, ' ', '"'); foreach($args as $arg){ } } $args = $tmp; } $out[$key] = $args[$i+1]; $i++; $out[$key] = $parts[1]; }else{ $out[$key] = true; } $last_arg = $key; for( $j = 0, $jl = $len; $j < $jl; $j++ ){ $key = $match[1]{$j}; $val = ($args[$i+1]) ? $args[$i+1]: true; $out[$key] = ($match[0]{$len} == $match[1]{$j}) ? $val : true; } $last_arg = $key; $key = $match[0]; $out[$key] = true; $last_arg = $key; }else if($last_arg !== null) { $out[$last_arg] = $args[$i]; } } return $out; } $str = 'yankee -D "oo\"d l e\'s" -went "2 town 2 buy him-self" -a pony --calledit=" \"macaroonis\' "'; /* Will output: array(9) { ["yankee"]=> bool(true) ["D"]=> string(10) "oo"d l e's" ["w"]=> bool(true) ["e"]=> bool(true) ["n"]=> bool(true) ["t"]=> string(21) "2 town 2 buy him-self" ["a"]=> string(4) "pony" ["pony"]=> bool(true) ["calledit"]=> string(12) ""macaroonis'" } */
URL: http://snipplr.com/view/6677/