Changeset 651 for branches/1.1dev/lib


Ignore:
Timestamp:
Nov 16, 2018 2:32:21 AM (5 years ago)
Author:
anonymous
Message:

Update fancyDump

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/Utilities.inc.php

    r650 r651  
    7777}
    7878
    79 /**
    80  * Return dump as cleaned text. Useful for dumping data into emails.
    81  *
    82  * @param  array    $var        Variable to dump.
    83  * @param  strong   $indent     A string to prepend indented lines (tab for example).
    84  * @return string Dump of var.
    85  */
    86 function fancyDump($var, $indent='')
    87 {
     79/*
     80* Return dump as cleaned text. Useful for dumping data into emails or output from CLI scripts.
     81* To output tab-style lists set $indent to "\t" and $depth to 0;
     82* To output markdown-style lists set $indent to '- ' and $depth to 1;
     83* Also see yaml_emit() https://secure.php.net/manual/en/function.yaml-emit.php
     84*
     85* @param  array    $var        Variable to dump.
     86* @param  string   $indent     A string to prepend indented lines.
     87* @param  string   $depth      Starting depth of this iteration of recursion (set to 0 to have no initial indentation).
     88* @return string               Pretty dump of $var.
     89* @author   Quinn Comendant <quinn@strangecode.com>
     90* @version 2.0
     91*/
     92function fancyDump($var, $indent='- ', $depth=1)
     93{
     94    $indent_str = str_repeat($indent, $depth);
    8895    $output = '';
    8996    if (is_array($var)) {
     
    9198            $k = ucfirst(mb_strtolower(str_replace(array('_', '  '), ' ', $k)));
    9299            if (is_array($v)) {
    93                 $output .= sprintf("\n%s%s: %s\n", $indent, $k, fancyDump($v, $indent . $indent));
     100                $output .= sprintf("\n%s%s:\n%s\n", $indent_str, $k, fancyDump($v, $indent, $depth+1));
    94101            } else {
    95                 $output .= sprintf("%s%s: %s\n", $indent, $k, $v);
     102                $output .= sprintf("%s%s: %s\n", $indent_str, $k, $v);
    96103            }
    97104        }
    98105    } else {
    99         $output .= sprintf("%s%s\n", $indent, $var);
    100     }
    101     return preg_replace(['/^[ \t]+$/', '/\n\n\n+/'], ['', "\n\n"], $output);
     106        $output .= sprintf("%s%s\n", $indent_str, $var);
     107    }
     108    return preg_replace(['/^[ \t]+$/', '/\n\n+/', '/^(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(\S )/m'], ['', "\n", '$1$1$2$2$3$3$4$4$5$5$6$6$7$7$8$8$9'], $output);
    102109}
    103110
Note: See TracChangeset for help on using the changeset viewer.