note 68696 deleted from function.print-r by nlopess
| From: | nlopess@php.net | Date: | Tue, 08 Aug 2006 19:23:55 +0000 |
| Subject: | note 68696 deleted from function.print-r by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115680@lists.php.net to get a copy of this message | ||
Note Submitter: rd at newsfare dot com
----
Fancy version of print_r:
function prettyprint_array($theVar) {
$newline_tok = "#new line#";
$ret_tok = "#ret#";
$html_br_tok = "#html br#";
$space_tok = "#s#";
$lparen_tok = "#LEFTPAREN#";
$rparen_tok = "#RIGHTPAREN#";
// This is the pattern of vertical bars which will make indent levels visible.
$space_decoration = '| ';
// Get an indented listing of the array structure and contents.
$theOutput = nl2br(print_r($theVar, TRUE));
// For uniformity, prepend one space to each line.
$theOutput = preg_replace( "@^@", " ", $theOutput);
// Replace parens with tokens.
$theOutput = preg_replace("@\(@", $lparen_tok, $theOutput);
$theOutput = preg_replace("@\)@", $rparen_tok, $theOutput);
// Replace space with a token.
$theOutput = preg_replace("@ @", $space_tok, $theOutput);
// Replace <br /> with a token.
$theOutput = preg_replace( "@\<br$space_tok/>@ms", $html_br_tok, $theOutput);
// Replace newline with a token.
$theOutput = preg_replace( "@\n@ms", $newline_tok, $theOutput);
// Replace carriage return with a token.
$theOutput = preg_replace( "@\r@ms", $ret_tok, $theOutput);
// Unwrap wrapped lines.
$theOutput = preg_replace("@$html_br_tok$ret_tok$newline_tok@", " ",
$theOutput);
// Remove lines which consist solely of spaces and parentheses. Such lines are unnecessary.
//#html br##new line#%%%%%%%%%%%%%%%%#RIGHTPAREN#
$theOutput = preg_replace("@$html_br_tok$newline_tok($space_tok)+$rparen_tok@",
"", $theOutput);
$theOutput = preg_replace("@$html_br_tok$newline_tok($space_tok)+$lparen_tok@",
"", $theOutput);
// Change two line breaks with only one character in between to one line break.
// #html br##new line##html br##new line#
$theOutput = preg_replace("@$html_br_tok$newline_tok$html_br_tok$newline_tok@",
"$html_br_tok$newline_tok", $theOutput);
$theOutput = preg_replace("@$html_br_tok$newline_tok$html_br_tok$newline_tok@",
"$html_br_tok$newline_tok", $theOutput);
$theOutput = str_replace ( $newline_tok, "\n", $theOutput);
$theOutput = str_replace ( $ret_tok, "\r", $theOutput);
$theOutput = str_replace ( $html_br_tok, "<br>", $theOutput);
$theOutput = str_replace ( $space_tok, " ", $theOutput);
$theOutput = str_replace ( $lparen_tok, "(", $theOutput);
$theOutput = str_replace ( $rparen_tok, ")", $theOutput);
// Change leading spaces to a pattern, first substituting pound signs for the spaces.
$theOutput = preg_replace("@ @", "#", $theOutput);
// Surround the soon-to-be-a-pattern (now #) area with a styled span container.
$theOutput = preg_replace("@(#+)@", "<span
style='color:rgb(50%,50%,90%)'>$1</span>", $theOutput);
// Prepend an indent count to each row.
$theOutput = str_replace('##########', "9
&&&&&&&", $theOutput);
$theOutput = str_replace('#########', "8
&&&&&&&", $theOutput);
$theOutput = str_replace('########', "7 &&&&&&",
$theOutput);
$theOutput = str_replace('#######', "6 &&&&&",
$theOutput);
$theOutput = str_replace('######', "5 &&&&", $theOutput);
$theOutput = str_replace('#####', "4 &&&", $theOutput);
$theOutput = str_replace('####', "3 &&", $theOutput);
$theOutput = str_replace('###', "2 &", $theOutput);
$theOutput = str_replace('##', "1 ", $theOutput);
$theOutput = str_replace('#', "0 ", $theOutput);
// Fill in a pattern of vertical bars, etc., to show the indent columns.
$theOutput = preg_replace("@&@", "$space_decoration", $theOutput);
// Make the output of more readable.
echo '<div style="background-color:rgb(95%,95%,100%); font-size:9pt; font-family:
courier new, courier, monospace">';
echo "$theOutput</div>";
echo '<div style="background-color:rgb(95%,95%,100%); font-size:9pt; font-family:
courier new, courier, monospace">';
}