尝试在php中为命令行转义时出错

时间:2021-02-06 23:17:40

I am starting with

我开始了

http://myurl.com/helloworld.html","asdfa

and I am expecting after my regex is run

在我的正则表达式运行之后,我期待着

http://myurl.com/helloworld.html

I have the following php command

我有以下php命令

echo shell_exec("cat /tmp/urls | grep -Eo '^[^"]+'");

But I get no result when it runs.

但是当它运行时我没有得到任何结果。

When I run straight from command line with the following it works

当我直接从命令行运行时,它可以正常工作

$cat /tmp/urls | grep -Eo '^[^"]+'

1 个解决方案

#1


2  

The \" in your php-string is actually ", so if you want it to actually be \" you should escape both the double-quote " and the slash \:

“你的php-string实际上是”,所以如果你想要它实际上是“你应该逃避双引号”和斜杠\:

echo shell_exec("cat /tmp/urls | grep -Eo '^[^\\\"]+'");

Another option is to use a single quote (and not double), however note that here you do need to escape the single-quotes inside the string:

另一种选择是使用单引号(而不是double),但请注意,在这里你需要转义字符串中的单引号:

echo shell_exec('cat /tmp/urls | grep -Eo \'^[^\"]+\'');

update

Based on the comment your code should be:

根据您的代码应该是注释:

echo shell_exec("cat /tmp/urls | grep -Eo '^[^\"]+'");

#1


2  

The \" in your php-string is actually ", so if you want it to actually be \" you should escape both the double-quote " and the slash \:

“你的php-string实际上是”,所以如果你想要它实际上是“你应该逃避双引号”和斜杠\:

echo shell_exec("cat /tmp/urls | grep -Eo '^[^\\\"]+'");

Another option is to use a single quote (and not double), however note that here you do need to escape the single-quotes inside the string:

另一种选择是使用单引号(而不是double),但请注意,在这里你需要转义字符串中的单引号:

echo shell_exec('cat /tmp/urls | grep -Eo \'^[^\"]+\'');

update

Based on the comment your code should be:

根据您的代码应该是注释:

echo shell_exec("cat /tmp/urls | grep -Eo '^[^\"]+'");