modifier.debug_print_var.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty debug_print_var modifier plugin
  9. *
  10. * Type: modifier<br>
  11. * Name: debug_print_var<br>
  12. * Purpose: formats variable contents for display in the console
  13. * @link http://smarty.php.net/manual/en/language.modifier.debug.print.var.php
  14. * debug_print_var (Smarty online manual)
  15. * @param array|object
  16. * @param integer
  17. * @param integer
  18. * @return string
  19. */
  20. function smarty_modifier_debug_print_var($var, $depth = 0, $length = 40)
  21. {
  22. $_replace = array("\n"=>'<i>&#92;n</i>', "\r"=>'<i>&#92;r</i>', "\t"=>'<i>&#92;t</i>');
  23. if (is_array($var)) {
  24. $results = "<b>Array (".count($var).")</b>";
  25. foreach ($var as $curr_key => $curr_val) {
  26. $return = smarty_modifier_debug_print_var($curr_val, $depth+1, $length);
  27. $results .= "<br>".str_repeat('&nbsp;', $depth*2)."<b>".strtr($curr_key, $_replace)."</b> =&gt; $return";
  28. }
  29. } else if (is_object($var)) {
  30. $object_vars = get_object_vars($var);
  31. $results = "<b>".get_class($var)." Object (".count($object_vars).")</b>";
  32. foreach ($object_vars as $curr_key => $curr_val) {
  33. $return = smarty_modifier_debug_print_var($curr_val, $depth+1, $length);
  34. $results .= "<br>".str_repeat('&nbsp;', $depth*2)."<b>$curr_key</b> =&gt; $return";
  35. }
  36. } else if (is_resource($var)) {
  37. $results = '<i>'.(string)$var.'</i>';
  38. } else if (empty($var) && $var != "0") {
  39. $results = '<i>empty</i>';
  40. } else {
  41. if (strlen($var) > $length ) {
  42. $results = substr($var, 0, $length-3).'...';
  43. } else {
  44. $results = $var;
  45. }
  46. $results = htmlspecialchars($results);
  47. $results = strtr($results, $_replace);
  48. }
  49. return $results;
  50. }
  51. /* vim: set expandtab: */
  52. ?>