note 86932 modified in function.debug-print-backtrace by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Nov 2008 16:24:48 +0000 |
| Subject: | note 86932 modified in function.debug-print-backtrace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146394@lists.php.net to get a copy of this message | ||
I like the output of debug_print_backtrace() but I sometimes want it as a string.
bortuzar's solution to use output buffering is great, but I'd like to factorize that into
a function. Doing that however always results in whatever function name I use appearing at the top
of the stack which is redundant.
Below is my noddy (simple) solution. If you don't care for renumbering the call stack, omit
the second preg_replace().
<?php
function debug_string_backtrace() {
ob_start();
debug_print_backtrace();
$trace = ob_get_contents();
ob_end_clean();
// Remove first item from backtrace as it's this function which
// is redundant.
$trace = preg_replace ('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '',
$trace, 1);
// Renumber backtrace items.
$trace = preg_replace ('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);
return $trace;
}
?>
--was--
I like the output of debug_print_backtrace() but I sometimes want it as a string.
bortuzar's solution to use output buffering is great, but I'd like to factorize that into
a function. Doing that however always results in whatever function name I use appearing at the top
of the stack which is redundant.
Below is my noddy (simple) solution. If you don't care for renumbering the call stack, omit
the second preg_replace().
function debug_string_backtrace() {
ob_start();
debug_print_backtrace();
$trace = ob_get_contents();
ob_end_clean();
// Remove first item from backtrace as it's this function which
// is redundant.
$trace = preg_replace ('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '',
$trace, 1);
// Renumber backtrace items.
$trace = preg_replace ('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);
return $trace;
}
http://php.net/manual/en/function.debug-print-backtrace.php