Newbie question about PHP CLI and STDIN

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Newbie question about PHP CLI and STDIN

Post by Chris Corbyn »

How can a ask the user to enter a bit of text in STDIN but present them with a default value (for example, the one they last used) which they can either edit, backspace out or just use?

Hopefully this example will get over what I mean:

Code: Select all

<?php

echo "Enter something: ";

fwrite(STDIN, 'Foo');

$line = fgets(STDIN); //User sees "Enter something: Foo -- but godammit they can't backspace the word Foo out again"

echo "\n\n$line";

?>
I don't want to have to use readline or ncurses and I'm guessing it's not even needed.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

never seen this used before.. are you making a console php app?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Most linux applications do something like this..

Code: Select all

port (default 25):
Then if you press enter, without typing anything, it defaults to 25. Maybe you can do it like that?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

astions wrote:Most linux applications do something like this..

Code: Select all

port (default 25):
Then if you press enter, without typing anything, it defaults to 25. Maybe you can do it like that?
I thought of that but I wanted the user to be able to make minor edits to the previous text entered -- it's for naming stuff and the names vary only slightly.

I guess I'll use readline since I have PHP set up with it but I'll have to learn how to use it :( Fun fun fun :) Thanks guys.
Post Reply