我注意到每次我使用print_r
一些数组时,我都无法正确获得显示样式,我的意思是它在浏览器客户端视图中仅显示在一行中。您是否有任何工具、方法或任何方法可以帮助以可读的格式更好地显示它?
问问题
886 次
7 回答
3
尝试
echo'<pre>';
print_r();
echo'</pre>';
它会给你一个格式良好的输出
于 2012-09-12T04:28:09.613 回答
2
以管理良好的方式输出数组,html标签<pre>
很有用尝试
<pre>
<?php print_r() ?>
</pre>
于 2012-09-12T04:47:29.500 回答
2
这个问题也让我很沮丧,每次调试我都会写 3 行代码......
echo '<pre>';
print_r($array);
echo '</pre>';
最后我只写了一个函数(print_a)来为我做这件事。多年来,此功能已扩展,我向您介绍 print_r 类固醇。
如果我进行任何更新或者您有任何要添加的内容,我会在此处发布 - 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"), ' ', $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;
}
于 2013-05-07T19:10:19.377 回答
1
我喜欢创建一个名为 pr 的包装函数,它在 print_r 的输出之前和之后插入一个 pre 标记
IE
function pr($variable) {
echo '<pre>';
print_r($variable);
echo '</pre>';
}
于 2012-09-12T04:30:57.180 回答
0
echo'<pre>';
print_r();
echo'</pre>';
或者,如果您没有其他要输出的内容:
header("Content-Type: text/plain");
print_r();
于 2012-09-12T04:30:30.053 回答
0
这是因为浏览器在呈现文本时会将空格剥离为单个空格,但您不需要像其他人建议的那样需要三行:
echo '<pre>', print_r($var, true), '<pre>';
于 2012-09-12T04:38:47.330 回答
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;
}
于 2013-03-29T00:21:45.327 回答