user3383675
user3383675

Reputation: 1081

How to return carriage to previous line in PHP

The \r will move my pointer to the begin of line, but how to return to previous line??

e.g. From code:

echo "First Line \n";
echo "Second Line \n";
ReturnToPreviousLine();
echo "Third Line \n"; 

executed by $ php script.php

I want to output in console:

Third Line
Second Line

Maybe I will add better example:

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

Output during loop:

Hello World!
Time        Percent
00:00:10    10%

Output after loop:

Hello World!
Done

Upvotes: 1

Views: 2134

Answers (3)

Meyer Auslander - Tst
Meyer Auslander - Tst

Reputation: 23

Here is a nice instructional example taken from https://www.hashbangcode.com/article/overwriting-command-line-output-php

function replaceCommandOutput(array $output) {
  static $oldLines = 0;
  $numNewLines = count($output) - 1;

  if ($oldLines == 0) {
    $oldLines = $numNewLines;
  }

  echo implode(PHP_EOL, $output);
  echo chr(27) . "[0G";
  echo chr(27) . "[" . $oldLines . "A";
}

while (true) {
  $output = [];
  $output[] = 'First Line';
  $output[] = 'Time: ' . date('r');
  $output[] = 'Random number: ' . rand(100, 999);
  $output[] = 'Random letter: ' . chr(rand(65, 89));
  $output[] = 'Last Line';
  replaceCommandOutput($output);
  usleep(100000);
}

To go to the start of the line use echo chr(27) . "[0G";

To go up a number of lines use echo chr(27) . "[" . $numLines . "A";

Upvotes: 0

mTorres
mTorres

Reputation: 3590

You can give it a try with output buffering, AFAIK if you don't use it the moment you call echo you get your line, so you can't undo it.

So the trick is use output buffering and then prior to outputing it again, hook into the output to change what you want:

<?php

ob_start();

echo "First line\n";
echo "Second line\n";
echo "Third line\n";

$output = ob_get_clean();

$lines = explode("\n", $output);

print_r($lines);

// Outputs:
Array
(
    [0] => First line
    [1] => Second line
    [2] => Third line
    [3] =>
)

From here on you'll have your output in the $lines array, and then you can echo any line you want

Upvotes: -1

hek2mgl
hek2mgl

Reputation: 157947

You can achieve this visual output using ANSI terminal control chars. I once wrote a library for this, it is on Github: Jm_Console. I suggest installing it using PEAR (because it will handle depencies for you)

Here comes an example on how to use it:

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');

Upvotes: 5

Related Questions