I know I can go back the line and overwrite its contents with \r.
Now how can I go up into the previous line to change that?
Or is there even a way to print to a specific cursor location in the console window?
My goal is to create some self-refreshing multiline console app with PHP.
Use ANSI escape codes to move the cursor. For example:
Esc [ 1 F. To put the Escape character in a string you’ll need to specify its value numerically, for example “\x1B[1F”As sujoy suggests, you can use PHP ncurses for a more abstract way to move the cursor.
Whilst most “consoles” allow ANSI escape codes, other sorts of terminal use different character sequences,
ncursesprovides a standardised API that is terminal independent. Have a quick look at/etc/termcap(and thenman terminfo) if you are interested.Update: Lars Wirzenius’ answer has a useful summary of the background. Some years ago I also wrote a short article on terminals.