Question

I am using print_r(debug_backtrace(), true) to retrieve a string representation of the debug backtrace. This works fine, as print_r handles recursion.

When I tried to recursively iterate through the debug_backtrace() return array before turning it into a string it ran into recursion and never ended.

Is there some simple way I can remove certain sensitive key/value pairs from the backtrace array? Perhaps some way to turn the array to a string using print_r, then back to an array with the recursive locations changed to the string RECURSION, which I could the iterate through.

I don't want to execute regular expressions on the string representation if possible.

Was it helpful?

Solution

Aha... figured out that if I serialize the debug_backtrace array and then immemdiately unserialize it, the resulting array will lack the recursive references of the original array and I can safely recursively iterate through it.

$backtrace = debug_backtrace();
$backtrace = serialize($backtrace);
$backtrace = unserialize($backtrace);

recursive_sanitization_func($backtrace);

EDIT: Okay, so this isn't a complete solution. It works for recursive objects references (they are lost) but not for recursive arrays (they are retained).

My current solution is to serialize/unserialize as above, and then run the resulting array through a recursion-detection function such as that described here: http://www.php.net/manual/en/ref.array.php#96914

I wish there was something more straightforward but this is getting me by for now unless somebody else comes up with a better solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top