PHP Standard Input

PHP standard input?

It is possible to read the stdin by creating a file handle to php://stdin and then read from it with fgets() for a line for example (or, as you already stated, fgetc() for a single character):

<?php
$f = fopen( 'php://stdin', 'r' );

while( $line = fgets( $f ) ) {
echo $line;
}

fclose( $f );
?>

Reading line by line from STDIN

use STDIN constant as file handler.

while($f = fgets(STDIN)){
echo "line: $f";
}

Note: fgets on STDIN reads the \n character.

redirect a file contents to standard input in php


php yourscript.php < yourinputfile

should do the trick.

How to process stdin to stdout in php?

If you are piping, you will want to buffer the input, instead of processing it all at once, just go one line at a time as is standard for *nix tools.

The SheBang on top of the file allows you to execute the file directly, instead of having to call php in the command line.

Save the following to test.php and run

cat test.php | ./test.php

to see the results.

#!php
<?php
$handle = fopen('php://stdin', 'r');
$count = 0;
while(!feof($handle)) {
$buffer = fgets($handle);
echo $count++, ": ", $buffer;
}
fclose($handle);

How to take multiple standard input in a single line using php command line

Actually, the problem is in the following line:

$myPosition = (int) fgets(STDIN);

Here, the explicit conversion to int is discarding the value after space, so when you give 1 2 as the input in the command line the (int) is turning it into 1 and you are losing the other character.

$arr = [];
$testCase = (int) fgets(STDIN);

while ($testCase--) {
list($a, $b) = explode(' ', fgets(STDIN));
$arr[] = $a.' '.$b;
}

print_r($arr);

The above solution works because I've removed the (int) from the beginning of the fgets. Also note that, I'm using list($a, $b) here, which will actually create two variable $a and $b in the current scope so I'm always assuming that, you'll use two separate numbers (i.e: 1 2), otherwise you can use $inputParts = preg_split("/[\s]+/",$input) or something else with explode to form the array from input from console.

How to use STDOUT in php

Okay, let me give you another example for the STDIN and STDOUT usage.

In PHP you use these two idioms:

 $input = fgets(STDIN);

fwrite(STDOUT, $output);

When from the commandline you utilize them as such:

 cat "input.txt"  |  php script.php   >  "output.txt"

php script.php < input.txt > output.txt

echo "input..." | php script.php | sort | tee output.txt

That's all these things do. Piping in, or piping out. And the incoming parts will appear in STDIN, whereas your output should go to STDOUT. Never cross the streams, folks!

PHP - detect STDIO input


if(FALSE !== ftell(STDIN))
{
while (FALSE !== ($line = fgets(STDIN)))
{
$customLogArr[]=$line;
}
}

For STDIN, if nothing can be read, ftell() will return false.

How can I check if stdin exists in PHP ( php-cgi )?

The problem is that you create a endless loop with the while($line = fgets($fh)) part in your code.

$stdin = '';
$fh = fopen('php://stdin','r');
if($fh) {
// read *one* line from stdin upto "\r\n"
$stdin = fgets($fh);
fclose($fh);
}
echo $stdin;

The above would work if you're passing arguments like echo foo=bar | ./myscript.php and will read a single line when you call it like ./myscript.php
If you like to read more lines and keep your original code you can send a quit signal CTRL + D

To get parameters passed like ./myscript.php foo=bar you could check the contents of the $argv variable, in which the first argument always is the name of the executing script:

 ./myscript.php foo=bar

// File: myscript.php
$stdin = '';
for($i = 1; $i < count($argv); i++) {
$stdin .= $argv[$i];
}

I'm not sure that this solves anything but perhaps it give you some ideas.



Related Topics



Leave a reply



Submit