I would like to know how to split this string:
command arg1 arg2 arg3
In this array:
[arg1,arg2,arg3]
BUT if the user types:
command "arg1 still arg1" arg2
The array will be:
[arg1 still arg1, arg2]
Using PHP
I would like to know how to split this string:
command arg1 arg2 arg3
In this array:
[arg1,arg2,arg3]
BUT if the user types:
command "arg1 still arg1" arg2
The array will be:
[arg1 still arg1, arg2]
Using PHP
Use a regular expression like this:
^[^ ]+(?: ("[^"]+"|[^ ]+))*$
The expression "[^"]+"|[^ ]+
matches either an argument that starts and ends with a quotation mark, or an argument that ends at the next space.
I believe you might want to use something like strtok for this in php, rather than a regex.
See this comment on the documentation: http://www.php.net/manual/en/function.strtok.php#94463
PHP does what you ask automatically.
I have PHP 5.3, and calling this code
<?php
print $argc . "\n";
for ($i = 0; $i < $argc; $i++) {
print $argv[$i] . "\n";
}
?>
with php ./argv.php Test "Test 23" "Test 34"
, I obtain the following output
sunradio:~ kiamlaluno$ php ./argv.php Test "Test 23" "Test 34"
4
./argv.php
Test
Test 23
Test 34
sunradio:~ kiamlaluno$
Taking off the first argument, $argv
contains all the arguments as you want them. That can be easily done with array_pop()
.