PHP 8.5.0 Alpha 1 available for testing

stream_get_line

(PHP 5, PHP 7, PHP 8)

stream_get_lineLee una línea en un flujo

Descripción

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

stream_get_line() lee una línea en el recurso handle.

La lectura termina cuando se han leído length bytes, cuando se encuentra la cadena no vacía especificada por ending (pero no se incluirá en el valor devuelto), o si ocurre EOF: cualquiera de los tres que ocurra primero.

Esta función es casi idéntica a fgets() excepto que permite usar un delimitador de línea diferente de los caracteres estándar \n, \r y \r\n, y no devuelve el delimitador en sí.

Parámetros

stream

Un recurso válido de fichero.

length

El número máximo de bytes a leer desde el gestor. Los valores negativos no están soportados. Cero (0) significa el tamaño de chunk de socket por defecto, es decir, 8192 bytes.

ending

Un delimitador de cadena opcional.

Valores devueltos

stream_get_line() lee una línea de tamaño máximo length en el flujo stream o false en caso de error.

Ver también

  • fread() - Lectura del archivo en modo binario
  • fgets() - Recupera la línea actual a partir de la posición del puntero de archivo
  • fgetc() - Lee un carácter en un fichero
add a note

User Contributed Notes 2 notes

up
12
pk at ritm dot ru
15 years ago
fgets is faster but stream_get_line is more useful in a tcp server scripts.

when fgets reads some bytes from socket, where EOF is reached, it returns bool(false) same as stream_get_line

BUT if remote client drops connection, and server script will try to read some data with function fgets, function will return bool(false), and stream_get_line will return string(0) ""

so you can detect remote client disconnection with stream_get_line, and cannot with fgets
up
1
cool at user dot com
3 months ago
This function is very efficient to redirect streams. I have older scripts to compare, and recent versions of PHP are so fast, that sometimes 2 lines might get fed into one, and this happens only on PHP 8.3 and 8.4.

After digging, i found that throttling a bit is the best to do to maintain retro compatibility.

$line = stream_get_line($response, 1024, "\n");
usleep(10000);
To Top