Logan Bailey
Logan Bailey

Reputation: 7127

Parse string as if it were command line arguments

Is there easy way to parse a string so that the resulting array is the same as the $argv super global? Basically the inverse of that parse.

Upvotes: 0

Views: 132

Answers (1)

emdienn
emdienn

Reputation: 46

$argv is the space-delimited result of the command that was invoked, so if I've understood your question correctly,

explode(' ', $cmdString); should do what you're looking for.

Example:

$cmdString = 'foo.php bar baz bing';

$argv = explode(' ', $cmdString);
$argc = count($argv);

Rememer that as-per the docs, $argv[0] is the name of the script.

Upvotes: 3

Related Questions