#native_company# #native_desc#
#native_cta#

Piping output into your PHP scripts

By Joe Stump
on September 12, 2000

Here is a dirty little trick that lets you pipe output from grep, sed, awk, perl, etc. into your PHP scripts


#!/usr/local/bin/php -q
<?

$fp = fopen('/dev/stdin','r+');
$n = 1;
while(!feof($fp)){
    $line = trim(fgets($fp,4096));
     // do stuff to line here
     echo $n.' ==> '.$line."n";
     flush();
     ++$n;
}
fclose($fp);

?>

Now you can do something like this: “ls -alF | output.php” after you have “chmod +x outputl.php”

This is extremely useful when you need to do log processing, etc. on different boxes and you may need to parse it differently on each box. You can merely cat file.txt | output.php and presto you have output to stdout.