Is it possible to reference an output buffer at specific depth in PHP?
<p>We can get ob level. We can get length of current ob stack. But can we reference ob stack at level <em>n</em>? I would really benefit of getting length of the buffer at specific depth.</p>

<p>Suppose an example:</p>

<pre><code>$length = array();

ob_start();
echo "1";
$length[] = ob_get_length(); // length of current stack (depth 1) is 1
ob_end_clean(); 


ob_start();
echo "11";
ob_start();
echo "2";
$length[] = ob_get_length(); // length of current stack (depth 2) is 1
ob_end_clean();
ob_end_clean();

ob_start();
echo "111";
ob_start();
echo "22";
ob_start();
echo "3";
$length[] = ob_get_length(); // length of current stack (depth 3) is 1
ob_end_clean();
ob_end_clean();
ob_end_clean();

print_r($length);
</code></pre>

<p>Output is:</p>

<pre><code>Array
(
    [0] =&gt; 1
    [1] =&gt; 1
    [2] =&gt; 1
)
</code></pre>

<p>The length of each most deepest stack is 1. This is as expected.</p>

<p>My app has recursive output generation and some of output stacks have to know the length of parent system-generated stack. I cannot rely on using <code>ob_get_length()</code> right before opening a new stack simply because other people could wrap the generator in their own ob stack. And that will break the app.</p>

<p>Are there any options? Thanks.</p>

<h1>EDIT:</h1>

<p>To illustrate what I need to get:</p>

<pre><code>ob_start();
echo "111"; // &lt;-- this is the stack of interest
ob_start();
echo "22";
ob_start();
echo "3";
$length[] = ob_get_length(); // length of current stack (depth 3) is 1
$top_stack_len = get_length_of_ob_stack(1); // expected length here should be 3 (strlen("111") == 3)
ob_end_clean();
ob_end_clean();
echo "some more chars to change length of stack 1";
ob_end_clean();

echo $top_stack_len; // I'm expecting to see 3 here.
</code></pre>
