如何在 php 中解析“db2 列表实用程序显示详细信息”的输出


How to parse output from 'db2 list utilities show detail' in php?

我想要一个函数,它将返回最好的关联数组的多维数组。我将整个输出分为三个部分,以使其更好地阅读。

解析此类文本的最佳方法是什么?输出的位置是这样的

$output = array
  (
  "18"=>array(
    "type"=>"backup",
    "name"=>"PLC",
    "Description"=>"offline db",
    "state"=>"Executing",
    "Start Time"=>"05/16/2012 10:55:45.240272"
  ),  
  "17"=>array(
    "type"=>"restore",
    "name"=>"TYNDALE",
    "Description"=>"db",
    "state"=>"Executing",
    "Start Time"=>"05/16/2012 10:49:53.340805"
  ),
  "15"=>array(
    "type"=>"restore",
    "name"=>"carinya",
    "Description"=>"automatic incremental db",
    "state"=>"Executing",
    "Start Time"=>"05/16/2012 10:48:21.423945"
  )
  );

第一节 + 二 + 三 从 linux 命令返回 db2 list utilities show detail 。所以它将是 php 中的行数组。

第一节

ID                               = 18
Type                             = BACKUP
Database Name                    = PLC
Partition Number                 = 0
Description                      = offline db 
Start Time                       = 05/16/2012 10:55:45.240272
State                            = Executing
Invocation Type                  = User
Throttling:
   Priority                      = Unthrottled
Progress Monitoring:
   Estimated Percentage Complete = 0
      Total Work                 = 3320093100 bytes
      Completed Work             = 255380 bytes
      Start Time                 = 05/16/2012 10:55:45.240303

第二节

ID                               = 17
Type                             = RESTORE
Database Name                    = TYNDALE
Partition Number                 = 0
Description                      = db 
Start Time                       = 05/16/2012 10:49:53.340805
State                            = Executing
Invocation Type                  = User
Progress Monitoring:
      Completed Work             = 117444608 bytes
      Start Time                 = 05/16/2012 10:49:53.340819

第三节

ID                               = 15
Type                             = RESTORE
Database Name                    = CARINYA
Partition Number                 = 0
Description                      = automatic incremental db 
Start Time                       = 05/16/2012 10:48:21.423945
State                            = Executing
Invocation Type                  = User
Progress Monitoring:
   Phase Number                  = 1
      Total Work                 = 16781312 bytes
      Completed Work             = 16781312 bytes
      Start Time                 = 05/16/2012 10:48:21.423954
   Phase Number [Current]        = 2
      Description                = 20120513023104
      Completed Work             = 272633856 bytes
      Start Time                 = 05/16/2012 10:48:23.502822
   Phase Number                  = 3
      Description                = 20120514021520
      Completed Work             = 0 bytes
      Start Time                 = Not Started
所以我

使用的代码是

function db2listutilities(){
    global $db2Path;
    $status = array();
    $ProgressMonitoring=false;
    $cmd = $db2Path . "db2 list utilities show detail" ;
    unset($output);
    exec($cmd, $output);
    foreach ($output as $line){
        if( strpos($line,'Progress Monitoring:') !== false ){
            $ProgressMonitoring=true;
        }
        $tmp_array=explode("=", $line);
        if (count($tmp_array)>1){
            switch (trim($tmp_array[0])) {
                case 'ID':
                    $id=$tmp_array[1];
                    $ProgressMonitoring=false;
                break;
                case 'Type':
                    $status[$id]['Type']=$tmp_array[1];
                break;
                case 'Database Name':
                    $status[$id]['Database Name']=$tmp_array[1];
                break;
                case 'Description':
                    if (!$ProgressMonitoring) {$status[$id]['Description']=$tmp_array[1];}
                break;
                case 'Start Time':
                    if (!$ProgressMonitoring) {$status[$id]['Start Time']=$tmp_array[1];}
                break;
                case 'State':
                    $status[$id]['State']=$tmp_array[1];
                break;
            }
        }
    }
    $return=array();
    foreach ($status as $st) {
        $return[] = $st['Database Name']." - ".strtolower($st['Type'])." - ".$st['State']." - ".date("H:i",strtotime($st['Start Time']))."<BR>";
    }
    return implode("",$return);
}