473,382 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to read from stdin using PHP

In TCL it would be written this way:

puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar
I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}
I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil
Jul 17 '05 #1
6 24831

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
In TCL it would be written this way:

> puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil


A cursory search found this in the fopen() help:

// read_data()
// gets a line of data from STDIN and returns it
function read_data() {

$in = fopen("php://stdin", "r");
set_timeout();
$in_string = fgets($in, 255);
clear_timeout();
fclose($in);
return $in_string;
}

Enjoy.

Garp
Jul 17 '05 #2
Phil Powell wrote:
In TCL it would be written this way:

> puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session. What you can do to mimick this behavior (ie "Do
you really want to..") is to display the results of the fields entered
on a "verification" page and continue if everything looks correct -
executing the form.

Web-based (where your browser is your viewer) pages are static - in that
they stand alone and do not have interaction with the system unless
you submit a form to that system using either POST or GET - in which
case, you get another page with additional forms etc...

now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.
Jul 17 '05 #3
Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.


Um.. with respect to that shebang statement. Is that necessary? I've been
writing all of my files in PHP without it and while it works, I'd like my
code to be standardized. Is that really supposed to be there? I know it's a
perl thing -- didn't realize you use it in PHP too...

-- Matt
Jul 17 '05 #4
On Thu, 1 Jul 2004 16:29:23 -0400, "Matthew Del Buono" <Ma******@nospam.com>
wrote:
#!/usr/local/bin/php -q


Um.. with respect to that shebang statement. Is that necessary? I've been
writing all of my files in PHP without it and while it works, I'd like my
code to be standardized. Is that really supposed to be there? I know it's a
perl thing -- didn't realize you use it in PHP too...


It's not a Perl thing, it's a Unix thing, and is required if you want to be
able to run as "./script.php" rather than "php ./script.php".

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5
Michael Austin <ma*****@firstdbasource.com> wrote in message news:<eM*****************@newssvr23.news.prodigy.c om>...
Phil Powell wrote:
In TCL it would be written this way:

> > puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> > BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session.


[snip]

NO this is never going to be web-based; this is pure CLI scripting
version of PHP, thus, I am having to use stdin as if I were coding in
TCL, Java or even Shell Script. I wound up figuring it out though
using a variation of your example to do so in PHP.

Thanx
Phil
now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.

Jul 17 '05 #6
Phil Powell wrote:
Michael Austin <ma*****@firstdbasource.com> wrote in message news:<eM*****************@newssvr23.news.prodigy.c om>...
Phil Powell wrote:

In TCL it would be written this way:

>>>puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

>>>BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session.

[snip]

NO this is never going to be web-based; this is pure CLI scripting
version of PHP, thus, I am having to use stdin as if I were coding in
TCL, Java or even Shell Script. I wound up figuring it out though
using a variation of your example to do so in PHP.

Thanx
Phil


Glad I, ummm, err... Google could be of assistance :)...

now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>


Michael Austin.
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joe Burnett | last post by:
Hello, I am using the following perl read function to read form information from a client web page: read(STDIN, $GN_QUERY, 100); Everything works fine on a Linux/Apache server. I get the...
2
by: sriram pasham | last post by:
Hi Could any one tell me how to access(read) a registry key using javascript from a webpage(ASP page). Thanks in advance. Sriram *** Sent via Developersdex http://www.developersdex.com ***...
4
by: Rich Wallace | last post by:
Hi all, I have an XML document fed to me from a third party app: <?xml version="1.0" encoding="WINDOWS-1252" ?> <GatewayPlan xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
3
by: ifmusic | last post by:
Hi!, i have a "server-like" application: i use select to accept and recieve data from Sockets. I want to Add a menu to this app something like "pick an option" thing, but it has to show data...
1
by: jmprince01 | last post by:
i have the following code which works just fine: printf("Enter a string: \n"); fgets(ch, 1024, stdin); but i need it to read two lines. this quits as soon as i hit the enter key. how...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
4
by: Adam Funk | last post by:
I'm using this sort of standard thing: for line in fileinput.input(): do_stuff(line) and wondering whether it reads until it hits an EOF and then passes lines (one at a time) into the...
3
by: MSK | last post by:
Hi I am a newbie to ASP.NET and VBScript, currently I am designing a page to show all the mapped drives using client-side scripts.. I want to read all the mapped drive details (name and Remote...
1
by: Ferdinend | last post by:
Hi All, I know, there is a way to write/read xml using a DOM/SAX parser which is coded in C++. (i.e, *.cpp file) Can we write/read xml using .c file. Thanks in advance, Ferdinend
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.