var_export prettifier / visualizer

时间:2014-02-28 06:55:59

标签: php

我正在使用var_export在发生错误时将输出转储到日志。然而,由于结果是纯文本,我没有机会通过像krumo这样的库来推送它,所以我可以交互式地探索输出。

人们有什么方法可以使var_export文本更具可读性?

1 个答案:

答案 0 :(得分: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>";

我希望它有所帮助;)