如何编写转换器将PHP文件更改为Bash文件?

时间:2022-03-23 16:11:08

My so far not-so-bad version to implement this is:

到目前为止,我实现这一点的不太糟糕的版本是:

function bashFileConvert($file)
{
    return preg_replace('/([^\/\s]+\s+[^\/]+)(\/|$)/','"${1}"${2}',$file);
}

which mostly processes the problem when there is a space in the file name,like

当文件名中有空格时,主要处理问题,例如

$flie = '/usr/local/my test file.txt'

while will not be recognizable for Bash.

虽然Bash无法识别。

So I need to convert to

所以我需要转换为

$file = '/usr/local/"my test file.txt"'

before calling something like:

在打电话之前:

exec('ls ' . $file);

But there are still many other corner cases, like the quote and '&' problem.

但仍有许多其他的极端案例,如引用和'&'问题。

So, is there a ready version to do this job?

那么,有这个工作的现成版本吗?

==================================

Now I tried escapeshellarg(), but it is a little strange here:

现在我尝试了escapeshellarg(),但这里有点奇怪:

$file = '/usr/local/apache2/resumes_txt/5/San Francisco/qtzhang/Device "Engineer"/Job Resume Qintao Zhang.pdf.txt';
echo escapeshellarg($file);

D:\\test>php test.php
"/usr/local/apache2/resumes_txt/5/San Francisco/qtzhang/Device  Engineer /Job Resume Qintao Zhang.pdf.txt"

It seems with this function, a quote is replaced with a space.

看来这个函数,引号被替换为空格。

1 个解决方案

#1


The solution is to use the escapeshellarg function in PHP (http://uk.php.net/manual/en/function.escapeshellarg.php):

解决方案是在PHP中使用escapeshellarg函数(http://uk.php.net/manual/en/function.escapeshellarg.php):

$file = escapeshellarg('/usr/local/my test file.txt');

exec('ls ' . $file);

It will wrap quotes round it and escape quotes for you.

它将围绕它包装引号并为您转义引号。

#1


The solution is to use the escapeshellarg function in PHP (http://uk.php.net/manual/en/function.escapeshellarg.php):

解决方案是在PHP中使用escapeshellarg函数(http://uk.php.net/manual/en/function.escapeshellarg.php):

$file = escapeshellarg('/usr/local/my test file.txt');

exec('ls ' . $file);

It will wrap quotes round it and escape quotes for you.

它将围绕它包装引号并为您转义引号。