<?php

function mb_sprintf($format, ...$args) {
    $params = $args;
    $callback = function ($length) use (&$params) {
        $value = array_shift($params);
        return $length[0] + strlen($value) - mb_strwidth($value);
    };
    $format = preg_replace_callback('/(?<=%|%-)\d+(?=s)/', $callback, $format);
    return sprintf($format, ...$args);
} // function end 


function print_r_reverse($input) { // revert print_r output back to array
        $lines = preg_split('#\r?\n#', trim($input));
        if (trim($lines[ 0 ]) != 'Array' && trim($lines[ 0 ] != 'stdClass Object')) {
            // bottomed out to something that isn't an array or object
            if ($input === '') {
                return null;
            }
            return $input;
        } else {
            // this is an array or object, lets parse it
            $match = array();
            if (preg_match("/(\s{5,})\(/", $lines[ 1 ], $match)) {
                // this is a tested array/recursive call to this function
                // take a set of spaces off the beginning
                $spaces = $match[ 1 ];
                $spaces_length = strlen($spaces);
                $lines_total = count($lines);
                for ($i = 0; $i < $lines_total; $i++) {
                    if (substr($lines[ $i ], 0, $spaces_length) == $spaces) {
                        $lines[ $i ] = substr($lines[ $i ], $spaces_length);
                    }
                }
            }

            $is_object = trim($lines[ 0 ]) == 'stdClass Object';
            array_shift($lines); // Array
            array_shift($lines); // (
            array_pop($lines); // )

            $input = implode("\n", $lines);

            $matches = array();
            // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
            preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $input, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

            $pos = array();
            $previous_key = '';
            $in_length = strlen($input);

            // store the following in $pos:
            // array with key = key of the parsed array's item
            // value = array(start position in $in, $end position in $in)
            foreach ($matches as $match) {
                $key = $match[ 1 ][ 0 ];
                $start = $match[ 0 ][ 1 ] + strlen($match[ 0 ][ 0 ]);
                $pos[ $key ] = array($start, $in_length);
                if ($previous_key != '') {
                    $pos[ $previous_key ][ 1 ] = $match[ 0 ][ 1 ] - 1;
                }
                $previous_key = $key;
            }
            $ret = array();

            foreach ($pos as $key => $where) {
                // recursively see if the parsed out value is an array too
                $ret[ $key ] = print_r_reverse(substr($input, $where[ 0 ], $where[ 1 ] - $where[ 0 ]));
            }
            return $is_object ? (object)$ret : $ret;
        }
    } // function print_r_reverse($input)

function dhms($seconds, $mode=1) { // build 1d10h30s time output

    $days = floor($seconds/86400);
    $hrs = floor($seconds / 3600);
    $mins = (int) ($seconds / 60) % 60; 
    $sec = (int) ($seconds % 60);

    if($days>0){
        //echo $days;exit;
        $hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
        $hours = $hrs-($days*24);
        $return_days = $days."d";
        $hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
    }else{
        $return_days="";
        $hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
    }

    $mins = str_pad($mins,2,'0',STR_PAD_LEFT);
    $sec = str_pad($sec,2,'0',STR_PAD_LEFT);

    if ($mode!=1) {
     if ($return_days=="") { $return["day"]="00"; } else $return["day"]=$days;
     $return["hour"]=$hrs;
     $return["min"]=$mins; 
     $return["sec"]=$sec; 
     return $return;
    } // if ($mode!=1) 


    return $return_days.$hrs."h".$mins."m".$sec."s";
} // function end

function filesize_formated($path)
{
    $size = filesize($path);
    $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $power = $size > 0 ? floor(log($size, 1024)) : 0;
    return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}


?>