Current Location: Home> Function Categories> stream_get_line

stream_get_line

Read a line from the resource stream until the given delimiter
Name:stream_get_line
Category:Stream
Programming Language:php
One-line Description:Read a line of content from the file pointer and return the line of content

Function name: stream_get_line()

Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7

Function description: The stream_get_line() function reads a line of content from the file pointer and returns the line of content.

Usage: stream_get_line(resource $handle, int $length, string $ending = ?): string|false

parameter:

  • $handle: Required, represents the resource of the file pointer. Usually, the resource obtained after opening a file is used by the fopen() function.
  • $length: Required, indicating the maximum number of bytes to be read. Reading will stop if a newline is encountered or the maximum number of bytes is reached.
  • $ending: optional, indicating the line ending character. The default is "\n". Multiple characters can be used as line ending characters.

Return value:

  • Returns the read line content, and returns false if the end of the file is reached.

Example:

 $handle = fopen("example.txt", "r"); if ($handle) { $line = stream_get_line($handle, 1024, "\n"); echo $line; fclose($handle); }

In the example above, we open a file named "example.txt" and then use the stream_get_line() function to read a line from the file pointer and store it in the variable $line. Finally, we output the line content to the screen.

Note that if the file pointer reaches the end of the file or the number of bytes read reaches the specified maximum number of bytes, the stream_get_line() function stops reading and returns false. Therefore, in actual applications, we need to judge whether a row of content has been successfully read based on the return value.

Similar Functions
Popular Articles