473,473 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 24842

"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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.