终端传递包含空格的变量参数

3
在终端中如何传递包含空格的字符串作为参数。实际上会跳过空格后面的部分,只接受第一个单词作为参数。
$word = 'soccer ball'
shell_exec('casperjs test.js --word='.$word);

那么我应该如何避免空格,以便只运行这个命令?

casperjs test.js --word=soccer

2
http://php.net/manual/en/function.escapeshellarg.php - hakre
我已经编辑了问题,希望你将其发布为答案,因为这是最经济的做法。 - narek
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
2

尝试用引号将其括起来:

casperjs test.js --word="soccer ball"

2
在他的情况下,shell_exec('casperjs test.js --word = "'.$word.'");' - Ofir Baruch
谢谢大家,但我认为上面有一个错别字。 - narek

2

对于您所描述的情况(在shell中除了空格之外还有其他特殊字符),PHP有一个函数叫做escapeshellarg

$word    = 'soccer ball';
$command = sprintf('casperjs test.js --word=%s', escapeshellarg($word));
$result  = shell_exec($command);

我会注意保留$word的值作为一个参数:

casperjs test.js --word='soccer ball'

另请参阅:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,