无法在 exec 函数上添加双正斜杠 php
can't add double forward slashes on exec function php
目前我在 PHP 中遇到这个问题,我正在尝试制作一个简单的网页,在 shell 中执行一些代码并显示过程 运行
我要执行的代码是这个
wmic -U User%Passwd //192.168.1.3 "select Caption from Win32_Process"
在 shell 中它执行正确 (Ubuntu 14.04 LTS) 但在 PHP 中那些双斜线被当作注释,就像在这段代码中
exec('wmic -U User%Passwd //'.$IP.' "select Caption from Win32_Process"',$exit);
但是,双正斜杠 (//) 正在注释其余代码!
我如何重写该代码,以便 PHP 将这些双斜杠解释为代码的一部分而不是注释?
提前致谢
您需要使用反斜杠字符 \
转义斜杠 /
字符,因此您的新代码将是:
exec('wmic -U User%Passwd \/\/'.$IP.' "select Caption from Win32_Process"',$exit);
来自 PHP 手册:
The backslash character has several uses. Firstly, if it is followed
by a non-alphanumeric character, it takes away any special meaning
that character may have. This use of backslash as an escape character
applies both inside and outside character classes.
For example, if you want to match a "*" character, you write "*" in
the pattern. This applies whether or not the following character would
otherwise be interpreted as a meta-character, so it is always safe to
precede a non-alphanumeric with "\" to specify that it stands for
itself. In particular, if you want to match a backslash, you write
"\".
目前我在 PHP 中遇到这个问题,我正在尝试制作一个简单的网页,在 shell 中执行一些代码并显示过程 运行 我要执行的代码是这个
wmic -U User%Passwd //192.168.1.3 "select Caption from Win32_Process"
在 shell 中它执行正确 (Ubuntu 14.04 LTS) 但在 PHP 中那些双斜线被当作注释,就像在这段代码中
exec('wmic -U User%Passwd //'.$IP.' "select Caption from Win32_Process"',$exit);
但是,双正斜杠 (//) 正在注释其余代码!
我如何重写该代码,以便 PHP 将这些双斜杠解释为代码的一部分而不是注释?
提前致谢
您需要使用反斜杠字符 \
转义斜杠 /
字符,因此您的新代码将是:
exec('wmic -U User%Passwd \/\/'.$IP.' "select Caption from Win32_Process"',$exit);
来自 PHP 手册:
The backslash character has several uses. Firstly, if it is followed by a non-alphanumeric character, it takes away any special meaning that character may have. This use of backslash as an escape character applies both inside and outside character classes.
For example, if you want to match a "*" character, you write "*" in the pattern. This applies whether or not the following character would otherwise be interpreted as a meta-character, so it is always safe to precede a non-alphanumeric with "\" to specify that it stands for itself. In particular, if you want to match a backslash, you write "\".