PHP stream_get_line() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_get_line() reads one line (or one chunk) from a stream, stopping when it sees a custom ending string or when it reaches the maximum length.

It is like fgets(), but you choose the delimiter. The ending itself is not included in the returned string. Use it for pipe-separated fields, custom record markers, or ordinary newlines.

Syntax

Call stream_get_line() with the values you need. Start with this line.

PHP
$line = stream_get_line($stream, 1024, "\n");
CallWhat it does
stream_get_line($stream, 1024, "\n")Reads until a newline or 1024 bytes, without returning the newline.
stream_get_line($stream, 100, "|")Reads until a pipe character, useful for simple delimited data.
stream_get_line($stream, 50)With an empty ending, stops only at the length limit or EOF.

The $length argument is required and counts the maximum bytes to examine. If the ending never appears within that window, PHP returns the length-limited chunk.

Library declarations
PHP
stream_get_line(resource $stream, int $length, string $ending = ""): string|false

Parameters

ParameterDescription
streamRequired. The open stream to read from.
lengthRequired. Maximum number of bytes to read (must be greater than 0).
endingOptional. The delimiter string that ends the read. Defaults to an empty string.

Return Value

Return ValueDescription
stringThe text before the ending (ending not included), or a length-limited chunk.
falseReturned on failure or when there is nothing left to read.

PHP Examples

Example 1 — Split on a Pipe Delimiter

Store three fields separated by | and read them one at a time with stream_get_line().

PHP
<?php
$stream = fopen("php://memory", "r+");
fwrite($stream, "red|green|blue");
rewind($stream);

echo stream_get_line($stream, 100, "|"), PHP_EOL;
echo stream_get_line($stream, 100, "|"), PHP_EOL;
echo stream_get_line($stream, 100, "|"), PHP_EOL;
fclose($stream);

How It Works

  1. The memory stream holds the text "red|green|blue".
  2. Each stream_get_line() call stops at the next | and omits that character.
  3. Three calls print red, green, and blue on separate lines.
  4. The delimiter is consumed but not returned.

Example 2 — Respect the Length Limit

When the delimiter is farther away than $length, PHP returns only the limited chunk.

PHP
<?php
$stream = fopen("php://temp", "r+");
fwrite($stream, "ABCDEFGHIJ");
rewind($stream);

echo stream_get_line($stream, 4, "|"), PHP_EOL;
echo stream_get_line($stream, 4, "|"), PHP_EOL;
fclose($stream);

How It Works

  1. There is no | in the data, so the ending never matches.
  2. The first call returns at most 4 bytes: ABCD.
  3. The second call returns the next 4 bytes: EFGH.
  4. Length protects you from reading unbounded input in one go.

Example 3 — Read Lines Ending With Newline

Write two newline-separated lines and read each one without the trailing newline.

PHP
<?php
$stream = fopen("php://memory", "r+");
fwrite($stream, "first\nsecond\n");
rewind($stream);

echo stream_get_line($stream, 64, "\n"), PHP_EOL;
echo stream_get_line($stream, 64, "\n"), PHP_EOL;
fclose($stream);

How It Works

  1. Two lines are written with \n endings.
  2. stream_get_line() stops at each newline and drops it from the result.
  3. The first call returns "first", the second returns "second".
  4. This is handy when you want line text without caring about the line-break character.

Frequently Asked Questions

It reads from a stream until a custom ending string appears or until a maximum length is reached, and returns the text without the ending.
fgets() is built around newline endings. stream_get_line() lets you choose any delimiter string, such as "|" or "\r\n".
No. PHP consumes the ending but does not append it to the returned string.
PHP returns up to $length bytes. Keep calling until you hit false or finish the stream.
Yes. Any readable stream works, including php://memory, php://temp, and normal files.

Next: stream_get_meta_data()

stream_get_meta_data() returns an associative array of information about an open stream, s.

stream_get_meta_data() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful