如何在PHP中将托架返回到上一行

use*_*675 1 php

\r将我的指针移动到开始行的,但如何返回到前行?

例如来自代码:

echo "First Line \n";
echo "Second Line \n";
ReturnToPreviousLine();
echo "Third Line \n"; 
Run Code Online (Sandbox Code Playgroud)

由...执行 $ php script.php

我想在控制台输出:

Third Line
Second Line
Run Code Online (Sandbox Code Playgroud)

也许我会添加更好的例子:

  echo "Hello World!\n"
  //some loop
    echo "Time        Percent\n";
    echo "\r$time       $percent";
  //end loop
  returnToPreviousLine();
  echo "Done                 \n";
  echo "                     \n";
Run Code Online (Sandbox Code Playgroud)

循环期间的输出:

Hello World!
Time        Percent
00:00:10    10%
Run Code Online (Sandbox Code Playgroud)

循环后输出:

Hello World!
Done
Run Code Online (Sandbox Code Playgroud)

hek*_*mgl 5

您可以使用ANSI终端控制字符实现此可视输出.我曾为此写过一个库,它在Github上:Jm_Console.我建议使用PEAR安装它(因为它会为你处理依赖关系)

这里有一个如何使用它的例子:

require_once 'Jm/Autoloader.php';

// Will refactor the Singleton pattern once :)
$console = Jm_Console::singleton();

// Save the cursor position before printing the first line
$console->savecursor();

// Output the first and second line
$console->writeln('First Line');
$console->writeln('Second Line');

// Sleep a second ...
sleep(1);

// Return back to the first line
$console->restorecursor();

// Erase the first line
$console->stdout()->eraseln();

// Print the third line (And the second line again, unfortunately)
$console->writeln('Third line');
$console->writeln('Second line');
Run Code Online (Sandbox Code Playgroud)