Skip to content

Instantly share code, notes, and snippets.

@mtongnz
Created January 9, 2024 23:18
Show Gist options
  • Save mtongnz/8af69c2b3b010ec705222024d15adfe4 to your computer and use it in GitHub Desktop.
Save mtongnz/8af69c2b3b010ec705222024d15adfe4 to your computer and use it in GitHub Desktop.
This is a script to extract and split the args in PHP using the User Scripts plugin. It allows for args that have spaces and are enclosed in double quotes.
#!/usr/bin/php
<?php
#description=PHP - Get split args out
#argumentDescription=This is the description of the argument(s)
#argumentDefault="a b c" 1 2 3 "de" 4 5
# Split out args
$args = preg_split('/(".*?")/', $argv[1],-1,PREG_SPLIT_DELIM_CAPTURE);
for ($key=0; $key<count($args); $key++) {
$arg = trim($args[$key]);
if ($arg == '') {
array_splice( $args, $key, 1);
$key-=1;
} else if (!str_contains($arg,'"')) {
$a = explode(' ', $arg);
array_splice( $args, $key, 1, $a );
$key+=count($a)-1;
} else {
$args[$key] = str_replace('"', "", $arg);
}
}
# Echo args
echo "Args: $argv[1]\n\n";
print_r($args);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment