views:

52

answers:

1

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.

+1  A: 

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.

RenderIn
Intriguing. +1 for an interesting solution. I pondered your problem for a few minutes, but didn't come up with anything helpful.
zombat
I'm still trying to figure out why this works, as the PHP manual suggests that the recursive references should be retained: http://php.net/manual/en/function.serialize.php "You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost."
RenderIn