gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:05:06 25 4
gpt4 key购买 nike

在终端中如何将包含空格的字符串作为参数传递。它实际上跳过了空格之后的部分,只取第一个单词。

$word = 'soccer ball'
shell_exec('casperjs test.js --word='.$word);

那么我怎样才能转义它只运行这个命令的空白

casperjs test.js --word=soccer

最佳答案

对于您所描述的情况(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'

另见:

关于php - 终端从包含空格的变量传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com