print_r和var_dump

时间:2012-09-12 04:26:39

标签: php

我注意到每次print_r某个数组时,我都无法正确显示显示样式,我的意思是它只在浏览器客户端视图中显示一行。您有任何工具,方法或任何可以帮助以可读格式更好地显示它的方法吗?

7 个答案:

答案 0 :(得分:3)

尝试

echo'<pre>';
print_r();
echo'</pre>';

它会为您提供格式正确的输出

答案 1 :(得分:2)

以良好管理的方式输出数组,html标记<pre>非常有用,请尝试

<pre>
<?php print_r() ?>
</pre>

答案 2 :(得分:2)

这个问题也让我感到沮丧,每次调试我都会编写3行代码......

echo '<pre>';
print_r($array);
echo '</pre>';

最后我刚写了一个函数(print_a)来为我做这个。这个功能多年来已经扩展,我向你展示了类固醇的print_r。

Example output from print_a

如果我进行了任何更新,或者您有任何要添加的内容,我会在此处发布 - https://github.com/MikeGarde/smarter-js/blob/master/dev/php/debug.php

/**
 * Like print_r() but so much better! Removes HTML formatting from an array while using Google Prittify.
 *
 * @author  Mike Garde
 *
 * @param array    $array   Array you want to see.
 * @param boolean  $die     Should this kill the process when done?
 * @param boolean  $return  Do you want this echoed or returned
 *
 * @return string  $string  A view of an array but formatted for easy reading via HTML.
 */
function print_a($array=false, $die=true, $return=false) {

    if(!$return)
        $return = 0;

    if(!$array && !$return)
        $array = $GLOBALS;

    if(!$return) {
        $in = '';
        $dent = '    ';
    } elseif($return) {
        $in = str_repeat(' ', ($return*4));
        $dent = str_repeat(' ', ($return*4)+4);
    }
    $indent = $in.$dent;
    unset($dent);

    $result = ($return) ? ' ' : $in;
    $result.= ((is_array($array)) ? 'Array' : 'stdClass Object')." (\n";
    foreach($array as $key => $value) {

        $result.= $indent.'['. $key .'] => ';

        if(is_array($value) || is_object($value))
            $result.= print_a($value, false, $return+1);
        elseif(strlen($value) == 0)
            $result.= 'null';
        elseif(preg_match("/^[0-9]+$/", $value))
            $result.= $value;
        elseif(preg_match("/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/", $value))
            $result.= '<span title="'.date("D, M j, Y, g:i a", strtotime($value)).' | '.clean_time_diff($value).'">'.$value.'</span>';
        else {
            $value = addcslashes(htmlspecialchars($value), '\'');

            if(strlen($value) > 240) {
                $value = str_replace(array("\n", "\r"), array('<br />', ''), $value);
                $result.= '<details><summary>\''.rtrim(substr($value, 0, 80), '\x92').'\'</summary>\''.$value.'\'</details>';
            } else {
                $result.= '\''.$value.'\'';
            }
        }
        $result.= "\n";
    }
    $result.= $in.')';
    $result = str_replace(array('    ', "\t"), '&nbsp;&nbsp;&nbsp;&nbsp;', $result);

    if(!$return) {
        $string = '<script src="//google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>'.
                  '<script src="//google-code-prettify.googlecode.com/svn/trunk/src/lang-css.js"></script>'.
                  '<link rel="stylesheet" type="text/css" href="//google-code-prettify.googlecode.com/svn/trunk/src/prettify.css">'.
                  '<link href="//fonts.googleapis.com/css?family=Ubuntu+Mono" rel="stylesheet" type="text/css">'.
                  '<style>'.
                  'pre { background-color: #fff; font-family: \'Ubuntu Mono\', sans-serif; }'.
                  'li.L0, li.L1, li.L2, li.L3, li.L5, li.L6, li.L7, li.L8 { list-style-type: decimal; }'.
                  'ol { padding: 0 0 0 45px; }'.
                  'details, details summary { display: inline-block; }'.
                  'details[open] summary span { display: none; }'.
                  '</style>'.
                  '<pre class="prettyprint linenums">'. $result .'</pre>'.
                  '<script>prettyPrint();</script>';
        echo $string;

        if ($die) die();

    } else {
        return $result;
    }

}


/**
 * Called by preg_replace_callback
 *
 * @param array  $matches
 */
function addslashes_2_regex($matches){
    return ' => \''.addslashes($matches[1])."'\n";
}


/**
 * Returns an easly readable time difference.
 *
 * @author  Mike Garde
 *
 * @param string  $start  Start time OR previously calculated difference
 * @param string  $end    End time OR leave blank if using previously calculated difference
 *
 * @return string Clean and readable difference in time
 *
 *
 * @example echo clean_time_diff(strtotime('-8 hours -31 minutes'));
 * @example echo clean_time_diff('2013-05-03 10:15:41');
 * @example echo clean_time_diff('2015-01-01 00:00:00', '2013-05-03 10:15:41');
 */
function clean_time_diff($start, $end=false){

    if(!is_int($start)) $start = strtotime($start);
    if(!is_int($end)) $end = strtotime($end);

    $diff   = (($end == false) ? time() : $end) - $start;
    $tense  = ($diff > 0) ? 'ago' : 'in the future';
    $diff   = abs($diff);
    $return = '';

    // Now
    if($diff == 0)
        return 'now';

    // Seconds
    if($diff < 60) {
        $return = $diff.' second'. (($diff==1) ? '' : 's');

    // Minutes
    } elseif($diff < 3600) {
        $minutes = round($diff / 60);
        $return = $minutes .' minute'. (($minutes==1) ? '' : 's');

    // < 4 Hours
    } elseif($diff < 14400) {
        $hours = floor($diff / 3600);
        $minutes = round((($diff / 3600) - $hours) * 60);
        $append = ($minutes > 0) ? ', '.$minutes.' minute'.(($hours==1) ? '' : 's') : '';
        $return = $hours.' hour'.(($hours==1) ? '' : 's').$append;

    // Hours
    } elseif($diff < 86400) {
        $hours = round($diff / 3600);
        $return = $hours .' hours';

    // < 4 Days
    } elseif($diff < 345600) {
        $days = floor($diff / 86400);
        $hours = round((($diff / 86400) - $days) * 24);
        $append = ($hours > 0) ? ', '.$hours.' hour'.(($hours==1) ? '' : 's') : '';
        $return = $days.' day'.(($days==1) ? '' : 's').$append;

    // Days
    } elseif($diff < 2592000) {
        $days = round($diff / 86400);
        $return = $days.' day'.(($days==1) ? '' : 's');

    // < 4 Months
    } elseif($diff < 10511769) {
        $months = floor($diff / 2627942);
        $days = round((($diff / 2627942) - $months) * 30.416);
        $append = ($days > 0) ? ', '.$days.' day'.(($days==1) ? '' : 's') : '';
        $return = $months.' month'.(($months==1) ? '' : 's').$append;

    // Months
    } elseif($diff < 31536000) {
        $months = round($diff / 2627942);
        $return = $months.' month'. (($months==1) ? '' : 's');

    // Years
    } else {
        $years = floor($diff / 31536000);
        $months = round((($diff / 31536000) - $years) * 12);
        $append = ($months > 0) ? ', '.$months.' month'.(($months==1) ? '' : 's') : '';
        $return = $years.' year'.(($years==1) ? '' : 's').$append;

    }
    return $return.' '.$tense;
}

答案 3 :(得分:1)

我想创建一个名为pr的包装函数,它在print_r的输出之前和之后插入一个pre标签

即。

function pr($variable) {
    echo '<pre>';
    print_r($variable);
    echo '</pre>';
}

答案 4 :(得分:0)

echo'<pre>';
print_r();
echo'</pre>';

或者,如果您还没有输出任何其他内容:

header("Content-Type: text/plain");
print_r();

答案 5 :(得分:0)

这是因为浏览器在渲染文本时会将空格剥离到单个空格,但是您不需要像其他建议那样使用三行:

echo '<pre>', print_r($var, true), '<pre>';

答案 6 :(得分:0)

我用:

function view($object) {
   $object = print_r($object, true);
   $lines = explode("\n", $object);

   echo '<pre class="prettyprint linenums"><ol class="linenums">';
       foreach ($lines as $line ) {
           $output = preg_replace('"\[([^;]*)\]"', '<span class="assoc">$0</span>', $line);
           $output = preg_replace('"\=\>"', '<span class="tag">$0</span>', $output);
           echo "<li><span>$output</span></li>";
       }
   echo'</ul></pre>';
}

然后我设计了一下......这是我的CSS:

.tag { color: #aaaaaa; }
.assoc { color: teal; }

.prettyprint {
padding: 8px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
}
.prettyprint.linenums {
-webkit-box-shadow: inset 60px 0 0 #fbfbfc, inset 61px 0 0 #ececf0;
-moz-box-shadow: inset 60px 0 0 #fbfbfc, inset 61px 0 0 #ececf0;
box-shadow: inset 60px 0 0 #fbfbfc, inset 61px 0 0 #ececf0;
}

ol.linenums {
margin: 0 0 0 53px;
color: #aaaaaa;
}

ol.linenums li > span {
padding-left: 12px;
color: #666666;
text-shadow: 1px 1px 1px white;
line-height: 20px;
text-shadow: 0 1px 0 #fff;
}