1

我正在运行这个脚本:

$output = "unrar x -y ".$number.".rar /bundle/";
echo "<pre>";
system($output);
echo "</pre>";

哪个在输出

提取 /var/www/html/bundle/Compressed_file_test/lgc-m.r29
0% 1% 2% OK

我想弄清楚它的文件夹名称,在本例中为“Compressed_file_test”。有没有办法我可以获得这些信息?

4

4 回答 4

3

你可以这样做:

$command = "unrar x -y ".$number.".rar /bundle/";
$output = shell_exec($command);
$output = explode(PHP_EOL, $output);
$output = $output[0];

preg_match_all('/\/[A-Za-z0-9_]+/', $output,  $matches);
echo $matches[1][count($matches[1])-2];

使用的功能:

于 2014-04-08T19:51:42.460 回答
1

您想将信息返回到变量中吗?您添加了第二个参数,但是,系统只返回最后一行。Shell?exec 返回所有内容

$result = shell_exec($output);
echo $result;

关于 shell_exec() 的文档


如果你想要这个值:

 $dir = stubst($output, strpos($output,"/",22), strrpos($output,"/"));
 // or via regex:
 preg_match('/\/(.*?) ?/', $output, $matches);
 $dir = $matches[4];
 // or if you know its the last:
 $dir = end($matches);

这是一个简单的例子,如果你得到更复杂的结果,你将不得不改变它(你可能想在换行符上爆炸以获得单独的行)

于 2014-04-08T19:46:04.110 回答
0

如果您想在代码中使用程序输出,请考虑使用exec()而不是 system()。

 $command = "unrar x -y $number.rar /bundle/";
 exec($command, $output, $return_var);

此外,请确保$number不包含任何特殊字符(如;),或转义它们。

于 2014-04-08T19:48:34.593 回答
0

像这样的东西:

$output = "unrar x -y ".$number.".rar /bundle/";
echo "<pre>";
system($output,$result);
preg_match(/\/(.*)/, $result, $path);
echo basename($path[0]);
echo "</pre>";
于 2014-04-08T20:33:39.627 回答