问题描述
- 如何格式化shell_exec返回的值
-
最近通过shell_exec执行zipinfo命令,得到对应的zip压缩包里面的文件列表信息,返回的是字符串,如何格式化输出返回值为数组形式?Archive: 222.zip
Zip file size: 1358592 bytes, number of entries: 69
-rw---- 6.3 fat 33897 bx defN 12-Jul-09 23:47 PEAR.php
-rw---- 6.3 fat 34308 bx defN 12-Jul-09 23:47 chmod.define
-rw---- 6.3 fat 2102 bx defN 12-Jul-09 23:47 chmod.php
-rw---- 6.3 fat 32 bx stor 12-Jul-09 23:47 config.php
-rw---- 6.3 fat 2356 bx defN 12-Oct-17 04:02 copy.php
-rw---- 6.3 fat 1991 bx defN 12-Jul-09 23:47 delete.php
-rw---- 6.3 fat 1659 bx defN 12-Jul-09 23:47 dirinfo.php
-rw---- 6.3 fat 371 bx defN 12-Jul-09 23:47 download.php
-rw---- 6.3 fat 1807 bx defN 12-Jul-09 23:47 editor.php
-rw---- 6.3 fat 609 bx defN 12-Jul-09 23:47 exit.php
-rw---- 6.3 fat 2788 bx defN 12-Sep-23 10:50 fileinfo.php
-rw---- 6.3 fat 3525 bx defN 12-Oct-17 03:46 function.php
drwx--- 6.3 fat 0 bx stor 16-Jan-25 09:54 geshi/
-rw---- 6.3 fat 16993 bx defN 12-Jul-09 23:47 geshi/apache.php
-rw---- 6.3 fat 6038 bx defN 12-Jul-09 23:47 geshi/asp.php
-rw---- 6.3 fat 10457 bx defN 12-Jul-09 23:47 geshi/c.php
-rw---- 6.3 fat 10103 bx defN 12-Jul-09 23:47 geshi/cpp.php
-rw---- 6.3 fat 9900 bx defN 12-Jul-09 23:47 geshi/css.php
-rw---- 6.3 fat 3016 bx defN 12-Jul-09 23:47 geshi/gettext.php
-rw---- 6.3 fat 6824 bx defN 12-Jul-09 23:47 geshi/html4strict.php
-rw---- 6.3 fat 7975 bx defN 12-Jul-09 23:47 geshi/html5.php
-rw---- 6.3 fat 50326 bx defN 12-Jul-09 23:47 geshi/java.php
22 files, 3552318 bytes uncompressed, 1349142 bytes compressed: 62.0%
解决方案
http://bbs.csdn.net/topics/391896428
解决方案二:
<?php
$string=shell_exec("zipinfo 222.zip");
file_put_contents('11.txt', $string);
$file = fopen("11.txt", "r") or exit("Unable to open file!");
if ($file) {
while (!feof($file)) {
$buffer = fgets($file, 4096);
if(strstr($buffer,'Archive')){
$zipname=end(explode(':',$buffer));
}elseif(strstr($buffer,'size:')){
$arr1=explode(':',$buffer);
$arr=explode(',',$arr1[1]);
$zipsize=$arr[0];
}elseif(strstr($buffer,'---')){
$arr=explode('bx',$buffer);
$name=end(explode(' ',end($arr)));
$sizestring=$arr[0];
$size=end(explode(' ', trim($sizestring)));
$buffer=$name.':'.$size;
$list=array(
'name' => $name,
'size' => $size,
);
}
}
$data['zipname']=$zipname;
$data['zipsize']=$zipsize;
$data['filelist']=$list;
print_r($data);
//echo json_decode($result);
fclose($file);
}
?>
最后输出来的数组,filelist只有一条list数据,怎么将所有的list都输出来?
解决方案三:
<?php
$string=shell_exec("zipinfo 222.zip");
file_put_contents('11.txt', $string);
$file = fopen("11.txt", "r") or exit("Unable to open file!");
if ($file) {
$i=0;
while (!feof($file)) {
$buffer = fgets($file, 4096);
if(strstr($buffer,'Archive')){
$zipname=end(explode(':',$buffer));
}elseif(strstr($buffer,'size:')){
$arr1=explode(':',$buffer);
$arr=explode(',',$arr1[1]);
$zipsize=$arr[0];
}elseif(strstr($buffer,'---')){
$arr=explode('bx',$buffer);
$name=end(explode(' ',end($arr)));
$sizestring=$arr[0];
$size=end(explode(' ', trim($sizestring)));
$buffer=$name.':'.$size;
$list[$i]=array(
'name' => $name,
'size' => $size,
);
}
$i++;
}
fclose($file);
$list=array_values($list);
$data['zipname']=$zipname;
$data['zipsize']=$zipsize;
$data['filelist']=$list;
//print_r($data);
echo json_encode($data);
}
搞定!