Reputation: 77
I am working on a PHP script which takes input from command line. it takes two strings as two country names like
- php myphp.php japan china (command line)
php code:
if(PHP_SAPI == 'cli') {
$first = $argv[1];
$second = $argv[2];
print_r($argv);
}
Here $first and $second are passed to other functions for further processing of the data. Before passing these arguments, I need to check if they are valid country names or not.
But when the country name consists of two strings like south korea, it does not take the whole country name as one input.
- php myphp.php japan south korea (command line)
Array
(
[0] => myphp.php
[1] => china
[2] => south
[3] => korea
)
How can i take input of a country which name has two or three strings ? e.g. in the above array, $argv[1] should be 'china' and $argv[2] should be 'south korea'.
Many thanks in advance !
Upvotes: 0
Views: 605
Reputation: 10897
You can simply enclose the two country names in quotes
- php myphp.php 'japan' 'south korea' (command line)
Array
(
[0] => cli.php
[1] => japan
[2] => south korea
)
Or if you want to use a comma you need to do some data manipulation in myphp.php
// concatenate the relevant arguments into one string
$arguments = '';
for ($i = 1; $i < count($argv); $i++) {
$arguments .= $argv[$i] . ' ';
}
// explode on comma
$arguments = explode(', ', $arguments);
print_r($arguments);
Then:
- php myphp.php japan, south korea (command line)
Array
(
[0] => japan
[1] => south korea
)
Upvotes: 2