Jump to content

PHP CLI Keep cursor on same line after user input


JChilds

Recommended Posts

Hello all,

 

I am trying to do something in PHP-CLI for hours now and cannot figure it out.

I want the user to be able to enter something (eg apples), and ON THE SAME LINE a response to be given.

 

My problem at the moment is the response is being put on a new line.

 

Is there a way to fix this?

 

eg, for the below code, the screen looks like this:

 

Apples

is a good idea!

 

Instead of:

 

Apples is a good idea!

 

 

<?php
//User to type something

$selection = fgets(STDIN);

echo " is a good idea!"

?>

Depends on the platform and your Unix shell (if Linux), but you could try echoing the VT220 escape sequences to control the cursor position.

echo "\033[*A";

 

You can also use these escape sequences to set colour as well:

echo "\033[41;30mblack on red\033[40;37m\r\n";

 

Hello, Thankyou

 

For now, I am testing this on a Windows Vista Machine.

 

The final product will eventually be on the smallest *nix platform I can find.

 

Are you 100% sure that this will work?

 

And/or is there a way to do it on windows machines?

 

 

No, I think it'll work on Linux if you're running a bash shell, or using telnet with emulation settings for VT220. But I can't make any guarantees.

 

Not that I'm aware of, but I've never tried to do it on a Windows machine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.