forked from delaking/PHP-Array-Beautifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_php_array_beautifier.php
107 lines (91 loc) · 3.12 KB
/
tool_php_array_beautifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/*
Template Name: Tool - PHP Array Beautifier
Author: Phillihp Harmon
Date: 2012.01.04
Contributor(s):
Description: Copy a print_r() or var_dump() string into the beautifier and clean it up, indent,
and add color codings for easy readability.
*/
?>
<?php
class Debug {
var $indent_size;
var $colors = array(
"Teal",
"YellowGreen",
"Tomato",
"Navy",
"MidnightBlue",
"FireBrick",
"DarkGreen"
);
function __construct() {
$this->indent_size = '20';
}
/*
* Author: Phil Harmon
* Description:
* Take an array and format it to style in HTML
*
* Tasks:
* - Add automated color formatting
*/
function array_to_html($val) {
$do_nothing = true;
// Get string structure
if(is_array($val)) {
ob_start();
print_r($val);
$val = ob_get_contents();
ob_end_clean();
}
// Color counter
$current = 0;
// Split the string into character array
$array = preg_split('//', $val, -1, PREG_SPLIT_NO_EMPTY);
foreach($array as $char) {
if($char == "[")
if(!$do_nothing)
echo "</div>";
else $do_nothing = false;
if($char == "[")
echo "<div>";
if($char == ")") {
echo "</div></div>";
$current--;
}
echo $char;
if($char == "(") {
echo "<div class='indent' style='padding-left: {$this->indent_size}px; color: ".($this->colors[$current % count($this->colors)]).";'>";
$do_nothing = true;
$current++;
}
}
}
}
$postdata = g('to_convert');
get_header();
?>
<div id="container" class="one-column">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div>This simple tool takes an array or object output in PHP, such as a print_r() statement and formats it in color coding to easily read your data.</div><br />
<?php if($postdata): ?>
<div><a href="http://phillihp.com/toolz/php-array-beautifier/">Go back</a></div>
<div style="width: 940px; overflow: auto;">
<?php
$d = new Debug();
$d->array_to_html($postdata);
?>
</div>
<?php endif; ?>
<form action="http://phillihp.com/toolz/php-array-beautifier/" method="POST">
<div>Code to Transform:</div>
<div><textarea id='to_conv' name='to_convert' rows='14' cols='120'><?php echo $postdata; ?></textarea></div>
<div><input type='submit' value='Beautify' /> <span onClick="document.getElementById('to_conv').value = 'Array([mode] => sets[sets] => Array([0] => 123[1] => 456[2] => 789)[etc] => Array([letters] => Array([0] => a[1] => b)[0] => pie[1] => sharks))';" style="color: blue; cursor: pointer;">Example</a></div>
</form>
<?php comments_template( '', true ); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>