Pretty print_r()


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $GLOBALS['ppr_mode'] = 'on';
  4.  
  5. function ppr_set_mode($mode) {
  6. // mode = html | off | log
  7. $GLOBALS['ppr_mode'] = $mode;
  8. }
  9.  
  10. function ppr() {
  11. if ($GLOBALS['ppr_mode'] == 'off') {
  12. return;
  13. }
  14. $bt = debug_backtrace();
  15. $file = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$bt[0]['file']);
  16. $line = $bt[0]['line'];
  17. $context = "ppr from $file($line)\n";
  18.  
  19. $args = _ppr_format_args(func_get_args());
  20. $html = _ppr_get_html($args);
  21. if ($GLOBALS['ppr_mode'] == 'log') {
  22. $html = $context . $html;
  23. error_log(_ppr_html_to_plain($html));
  24. }
  25. else {
  26. echo _ppr_get_style();
  27. echo '<pre class="ppr">';
  28. echo $context;
  29. echo $html;
  30. echo '</pre>';
  31. }
  32. }
  33.  
  34. function ppr_table($rows) {
  35. if ($GLOBALS['ppr_mode'] != 'html') {
  36. return;
  37. }
  38. echo _ppr_get_style();
  39. if (count($rows) == 0) {
  40. echo '<pre class="ppr">[Empty Table]</pre>';
  41. return;
  42. }
  43. echo '<pre class="ppr"><table class="ppr-rs" border="1"><tr>';
  44. foreach ($rows[0] as $th => $v) {
  45. echo "<th>$th</th>";
  46. }
  47. echo '</tr>';
  48. foreach ($rows as $row) {
  49. echo '<tr>';
  50. foreach ($row as $value) {
  51. echo '<td>' . _ppr_get_html($value, 1) . '</td>';
  52. }
  53. echo '</tr>';
  54. }
  55. echo '</table></pre>';
  56. }
  57.  
  58. function _ppr_get_object_vars($obj, $dump = '') {
  59. if (method_exists($obj, 'pprGetObjectVars')) {
  60. return $obj->pprGetObjectVars($dump);
  61. }
  62. $props = get_object_vars($obj);
  63. return $props;
  64. }
  65.  
  66. function _ppr_format_args($args) {
  67. if (count($args) == 1) {
  68. $args = $args[0];
  69. }
  70. else {
  71. $newArgs = array();
  72. foreach ($args as $i => $a) {
  73. $newArgs["@arg$i"] = $a;
  74. }
  75. $args = $newArgs;
  76. }
  77. return $args;
  78. }
  79.  
  80. function _ppr_get_html($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
  81. $out = '';
  82. if ($val === false || $val === true) {
  83. $out .= _ppr_get_html_bool($val);
  84. }
  85. elseif ($val === null) {
  86. $out .= _ppr_get_html_null();
  87. }
  88. elseif (is_resource($val)) {
  89. $out .= _ppr_get_html_resource($val);
  90. }
  91. elseif (is_array($val)) {
  92. $out .= _ppr_get_html_array($val, $_maxDepth, $_level, $_isInObject, $_objIdCache);
  93. }
  94. elseif (is_object($val)) {
  95. $out .= _ppr_get_html_object($val, $_maxDepth, $_level, $_isInObject, $_objIdCache);
  96. }
  97. elseif (is_float($val)) {
  98. $out .= _ppr_get_html_float($val);
  99. }
  100. elseif (is_int($val)) {
  101. $out .= _ppr_get_html_int($val);
  102. }
  103. else {
  104. $out .= _ppr_get_html_string($val);
  105. }
  106. if (!$_isInObject) {
  107. $out .= "<br />";
  108. }
  109. return $out;
  110. }
  111.  
  112. function _ppr_get_html_bool($val) {
  113. return '<span class="ppr-bool">' . ($val ? 'true' : 'false') . '</span>';
  114. }
  115.  
  116. function _ppr_get_html_null() {
  117. return '<span class="ppr-null">null</span>';
  118. }
  119.  
  120. function _ppr_get_html_resource($val) {
  121. var_dump($val);
  122. $dump = ob_get_clean();
  123. preg_match('/^.+\((\d+)\) of type \(([\w_ ]+)\)$/',$dump,$match);
  124. return '<span class="ppr-resource">Resource #' . $match[1] . ' <span class="ppr-resource-type">'.$match[2].'</span></span>';
  125. }
  126.  
  127. function _ppr_get_html_array($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
  128. $indent = str_repeat('&nbsp;&nbsp;&nbsp;',$_level);
  129. $indentNext = str_repeat('&nbsp;&nbsp;&nbsp;',$_level+1);
  130.  
  131. $out = '';
  132. if ($_maxDepth == 0) {
  133. $out .= '<span class="ppr-array">Array <span class="ppr-parens">(</span> <span class="ppr-max">Max Depth</span> <span class="ppr-parens">)</span></span>';
  134. }
  135. else {
  136. if (!$_isInObject) {
  137. $out .= '<span class="ppr-array">Array <span class="ppr-parens">(</span></span><br />';
  138. }
  139. foreach ($val as $k => $v) {
  140. $out .= $indentNext . '<span class="ppr-bracket">[<span class="ppr-array-key">' . htmlentities((string) $k,ENT_QUOTES,'utf-8') . '</span>]</span>';
  141. $out .= ' <span class="ppr-arrow">=&gt;</span> ' . _ppr_get_html($v, $_maxDepth-1, $_level+1, false, $_objIdCache);
  142. }
  143. if (!$_isInObject) {
  144. $out .= $indent . '<span class="ppr-parens">)</span>';
  145. }
  146. }
  147. return $out;
  148. }
  149.  
  150. function _ppr_get_html_object($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
  151. $indent = str_repeat('&nbsp;&nbsp;&nbsp;',$_level);
  152.  
  153. $out = '';
  154. var_dump($val);
  155. $dump = ob_get_clean();
  156. preg_match('/^\w+\(([\w_]+)\)#(\d+)/',$dump,$match);
  157. unset($dump);
  158. $class = $match[1];
  159. $id = $match[2];
  160. if ($_maxDepth <= 1 || isset($_objIdCache[$id])) {
  161. $text = (isset($_objIdCache[$id]) ? 'Recursion' : 'Max Depth');
  162. $out .= '<span class="ppr-object">Object #' . $id .' <span class="ppr-class">'.$class.'</span> <span class="ppr-brace">{</span> <span class="ppr-max">'.$text.'</span> <span class="ppr-brace">}</span></span>';
  163. }
  164. else {
  165. $_objIdCache[$id] = 1;
  166.  
  167. $props = _ppr_get_object_vars($val, $dump);
  168. $out .= '<span class="ppr-object">Object #' . $id .' <span class="ppr-class">'.$class.'</span> <span class="ppr-brace">{</span></span>';
  169. if (count($props)) {
  170. $out .= "<br />";
  171. $out .= _ppr_get_html($props,$_maxDepth-1,$_level,true,$_objIdCache);
  172. $out .= $indent;
  173. }
  174. $out .= '<span class="ppr-brace">}</span>';
  175. }
  176. return $out;
  177. }
  178.  
  179. function _ppr_get_html_float($val) {
  180. return '<span class="ppr-float">' . $val . '</span>';
  181. }
  182.  
  183. function _ppr_get_html_int($val) {
  184. return '<span class="ppr-int">' . $val . '</span>';
  185. }
  186.  
  187. function _ppr_get_html_string($val) {
  188. return '<span class="ppr-string"><span class="ppr-lquot">&ldquo;</span>' . htmlentities((string) $val,ENT_QUOTES) . '<span class="ppr-rquot">&rdquo;</span></span>';
  189. }
  190.  
  191. function _ppr_get_style() {
  192. static $style_printed;
  193. if ($style_printed) {
  194. return '';
  195. }
  196. $style_printed = true;
  197. return '
  198. <style type="text/css" media="screen">
  199. pre.ppr {
  200. font-family: "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace;
  201. font-size: 12px;
  202. line-height: 130%;
  203. word-wrap: break-word; /* IE 5.5 - 7 */
  204. white-space: pre-wrap; /* IE8, EOMB */
  205. background-color: rgb(255,255,255);
  206. background-color: rgba(255,255,255,0.92);
  207. border: 1px solid #888;
  208. padding: 5px;
  209. text-align: left;
  210. clear: both;
  211. }
  212. .ppr-array,
  213. .ppr-object,
  214. .ppr-resource {
  215. font-style: italic;
  216. }
  217. .ppr-brace,
  218. .ppr-parens {
  219. font-style: normal;
  220. }
  221. .ppr-string {
  222. color: #008800;
  223. font-weight: normal;
  224. }
  225. .ppr-lquot {
  226. color: #777;
  227. font-family: "Times New Roman", Times, serif;
  228. font-weight: bold;
  229. font-size: 10px;
  230. margin-right: 2px;
  231. }
  232. .ppr-rquot {
  233. color: #777;
  234. font-family: "Times New Roman", Times, serif;
  235. font-weight: bold;
  236. font-size: 10px;
  237. margin-left: 2px;
  238. }
  239. .ppr-arrow {
  240. color: #777;
  241. font-weight: bold;
  242. }
  243. .ppr-array-key {
  244. color: #303030;
  245. font-weight: normal;
  246. }
  247. .ppr-class,
  248. .ppr-resource-type {
  249. color: #0055ee;
  250. font-weight: bold;
  251. font-style: normal;
  252. }
  253. .ppr-null {
  254. color: #0000ee;
  255. font-style: italic;
  256. }
  257. .ppr-bool {
  258. color: #dd7700;
  259. font-style: italic;
  260. }
  261. .ppr-bracket {
  262. color: #888;
  263. }
  264. .ppr-float {
  265. color: #2200ee;
  266. font-weight: bold;
  267. }
  268. .ppr-int {
  269. color: #cc0000;
  270. font-weight: bold;
  271. }
  272. .ppr-max {
  273. text-transform: uppercase;
  274. font-style: italic;
  275. color: #bb6666;
  276. font-weight: bold;
  277. }
  278. </style>';
  279. }
  280.  
  281. function _ppr_html_to_plain($html) {
  282. $html = str_replace('&ldquo;','"', $html);
  283. $html = str_replace('&rdquo;','"', $html);
  284. $html = str_replace('&nbsp;'," ", $html);
  285. $html = str_replace('<br />',"\n", $html);
  286.  
  287. $str = strip_tags($html);
  288. $str = html_entity_decode($str);
  289. $str = trim($str);
  290. return $str;
  291. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.