obレベルを取得できます。現在のobスタックの長さを取得できます。しかし、レベルnでobスタックを参照できますか?特定の深さでバッファの長さを取得することは本当に有益です。
例を考えてみましょう:
$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);
出力は次のとおりです。
Array
(
[0] => 1
[1] => 1
[2] => 1
)
最も深いスタックの長さはそれぞれ1です。これは予想どおりです。
私のアプリには再帰的な出力生成があり、一部の出力スタックは、親システムで生成されたスタックの長さを認識している必要があります。ob_get_length()
他の人がジェネレータを自分のobスタックにラップできるという理由だけで、新しいスタックを開く直前に使用することに頼ることはできません。そして、それはアプリを壊します。
オプションはありますか?ありがとう。
編集:
私が取得する必要があるものを説明するために:
ob_start();
echo "111"; // <-- 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.