I need to convert this:
--foo -a --foobar="Hey there" --lol=laugh -dw randomtext texttext
Into this:
$switch = array ('-a' => true, '-d' => true, '-w' => true);
$parameter = array ('foo' => true, 'foobar' => "Hey there", 'lol' => "laugh");
$misc_args = array ('randomtext' => true, 'texttext' => true);
At the moment, I’m just exploding at spaces which makes $parameter['foobar'] impossible.
// Whatever's after the function name are arguements, we'll need those too
$argument = explode (' ', trim (preg_replace("/^".quotemeta ($function_name)."/", '', $_GET['r'])));
// These need to exist
$parameter = array ();
$misc_args = array ();
$switch = array ();
// If the first array element is empty, they're all empty so remove the array # This is caused by a weird bug, wherein the explode always finds space to explode...
if (empty ($argument[0])) unset ($argument); else {
// Look at each argument
foreach ($argument as $arg) {
// Now we need to work out which switches and arguements were included, there are two possible ones: --, and -
if ($arg[0] == $arg[1] && $arg[1] == "-") {
// This could be in the format simply "--hello" or "--hello=weclome", we need to find out which
if (strpos ($arg, '=') === FALSE) {
// Just the simple version
$parameter[trim ($arg, '-')] = TRUE;
} else {
// Split up the parameter and the value
$arr_arg = explode ('=', trim ($arg, '-'));
$parameter[$arr_arg[0]] = $arr_arg[1];
}
} elseif ($arg[0] == "-") {
// Go through each letter, and add it to the switch array
for ($x = 1; $x < strlen ($arg); $x++) $switch["-".$arg[$x]] = TRUE;
} else {
// This is just any other random junk
$misc_args[] = $arg;
}
}
}
I haven’t got time to check this out at the moment, but here’s a possible solution I got:
14:15 preg_replace(‘/((?(?=^).*?#OQ#|G)(?:(?!#CQ#).)*?)s+/g’, ‘$1#SP#’, $text)
14:16 though, it would probably be better to split it into two statements. One to
find all #OQ#..#CQ#, another to replace s+ with #SP#.
14:19 preg_replace(‘/(?< =#OQ#)(.*?)(?=#CQ#)/ge’,
‘preg_replace(‘/s+/’,'#SP#’,'$1′)’, $text) or something…
14:23 maybe move the second preg_replace into a function