Changeset 652 for trunk/lib


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

Update fancyDump

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r647 r652  
    125125}
    126126
    127 /**
    128  * Return dump as cleaned text. Useful for dumping data into emails.
    129  *
    130  * @param  array    $var        Variable to dump.
    131  * @param  strong   $indent     A string to prepend indented lines (tab for example).
    132  * @return string Dump of var.
    133  */
    134 function fancyDump($var, $indent='')
    135 {
     127/*
     128* Return dump as cleaned text. Useful for dumping data into emails or output from CLI scripts.
     129* To output tab-style lists set $indent to "\t" and $depth to 0;
     130* To output markdown-style lists set $indent to '- ' and $depth to 1;
     131* Also see yaml_emit() https://secure.php.net/manual/en/function.yaml-emit.php
     132*
     133* @param  array    $var        Variable to dump.
     134* @param  string   $indent     A string to prepend indented lines.
     135* @param  string   $depth      Starting depth of this iteration of recursion (set to 0 to have no initial indentation).
     136* @return string               Pretty dump of $var.
     137* @author   Quinn Comendant <quinn@strangecode.com>
     138* @version 2.0
     139*/
     140function fancyDump($var, $indent='- ', $depth=1)
     141{
     142    $indent_str = str_repeat($indent, $depth);
    136143    $output = '';
    137144    if (is_array($var)) {
     
    139146            $k = ucfirst(mb_strtolower(str_replace(array('_', '  '), ' ', $k)));
    140147            if (is_array($v)) {
    141                 $output .= sprintf("\n%s%s: %s\n", $indent, $k, fancyDump($v, $indent . $indent));
     148                $output .= sprintf("\n%s%s:\n%s\n", $indent_str, $k, fancyDump($v, $indent, $depth+1));
    142149            } else {
    143                 $output .= sprintf("%s%s: %s\n", $indent, $k, $v);
     150                $output .= sprintf("%s%s: %s\n", $indent_str, $k, $v);
    144151            }
    145152        }
    146153    } else {
    147         $output .= sprintf("%s%s\n", $indent, $var);
    148     }
    149     return preg_replace(['/^[ \t]+$/', '/\n\n\n+/'], ['', "\n\n"], $output);
     154        $output .= sprintf("%s%s\n", $indent_str, $var);
     155    }
     156    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);
    150157}
    151158
Note: See TracChangeset for help on using the changeset viewer.