Line Filters in PHP
Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. This example writes a capitalized version of all input text. You can use this pattern to write your own PHP line filters.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In this PHP version:
We use
fopen('php://stdin', 'r')
to open a stream for reading from standard input.We use a
while
loop withfgets()
to read lines from the input stream until we reach the end of the file (EOF).We use
strtoupper()
to convert each line to uppercase.We use
echo
to print the uppercased line.After the loop, we check if we’ve reached the end of the file. If not, it means an error occurred during reading.
Finally, we close the input stream with
fclose()
.
This script will behave similarly to the original Go program, reading lines from standard input, converting them to uppercase, and printing the result to standard output.