2014-02-28 74 views
0

我正在使用var_export將錯誤發生時將輸出轉儲到日誌。然而,由於結果是純文本的,我沒有機會通過像krumo這樣的庫來推送它,所以我可以交互式地探索輸出。var_export美化器/可視化器

人們有什麼方法可以使var_export文本更具可讀性?

回答

0

這裏是我的功能,它可以很好地用於多維數組:

function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") { 
    $res = ''; 
    if($short_syntax){ 
     $begin_array = '['; 
     $end_array = ']'; 
    } else { 
     $begin_array = 'array('; 
     $end_array = ')'; 
    } 
    $arr = explode('/',$varname); 
    $dim =count($arr)-1; 
    $lastKey = end($arr); 
    if (! is_array($varval)){ 
     if(is_string($varval)) $varval = "'$varval'"; 
     $res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line; 
    }else{ 

     $res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line; 
     $count_varval = 0; 
     $dim_varval = count($varval); 
     foreach ($varval as $key => $val){ 
      $count_varval++; 
      if($count_varval<$dim_varval) $commma=','; else $commma=''; 
      if(is_string($key)) $key = "'$key'"; 
      $res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma); 
     } 
     $res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line; 
    } 
    return $res; 
} 
$bigarray = array(); // your array 
$bb = VE ('$bigarray', $bigarray); 
echo "<pre>$bb</pre>"; 

我希望它能幫助;)