私はここで出力バッファリングを試していますが、改行や上書きで行き詰っています。
基本的に、このスニペットを CLI で実行すると:
<?php
$times = 5000;
for ($i = 1; $i <= $times; $i++)
{
echo chr(13) . sprintf('Running step %d/%d...', $i, $times);
}
行 1 にとどまり、内容を実際のステップ情報で上書きします。
同様に、最初のステップでは、コンソール出力は次のようになります。
> php micro.php
Running step 1/5000...
ステップ 3333:
> php micro.php
Running step 3333/5000...
完了後:
> php micro.php
Running step 5000/5000...
>
ご覧のとおり、合計で、プログラムは出力のために 1 行しか消費しません。
ここで、ブラウザ用のスクリプトを微調整して、ブラウザからリクエストすると、次のようになります。
<?php
header('Content-Type: text/plain; charset=iso-8859-1');
$times = 50000;
for ($i = 1; $i <= $times; $i++)
{
echo chr(13) . sprintf('Running step %d/%d...', $i, $times);
flush();
ob_flush();
}
スクリプトの処理中に出力を取得しますが、上書きされません。
同様に、最初のステップでは、コンソール出力は次のようになります。
localhost/micro.php:
Running step 1/5000...
ステップ 3333:
localhost/micro.php:
Running step 1/5000...
Running step 2/5000...
Running step 3/5000...
Running step 4/5000...
...
Running step 3333/5000...
完了後:
localhost/micro.php:
Running step 1/5000...
Running step 2/5000...
Running step 3/5000...
Running step 4/5000...
...
Running step 3333/5000...
...
Running step 5000/5000...
合計で 5001 行を消費します。
行の上書きを強制するためにブラウザー出力で改行するにはどうすればよいですか?