stream_get_line
从资源流里读取一行直到给定的定界符
函数名:stream_get_line()
适用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7
函数描述:stream_get_line() 函数从文件指针中读取一行内容,并返回该行内容。
用法: stream_get_line(resource $handle, int $length, string $ending = ?): string|false
参数:
返回值:
示例:
$handle = fopen("example.txt", "r");
if ($handle) {
$line = stream_get_line($handle, 1024, "\n");
echo $line;
fclose($handle);
}
在上面的示例中,我们打开了一个名为 "example.txt" 的文件,然后使用 stream_get_line() 函数从文件指针中读取一行内容,并将其存储在变量 $line 中。最后,我们将该行内容输出到屏幕上。
请注意,如果文件指针到达文件结尾或读取的字节数达到了指定的最大字节数,stream_get_line() 函数将停止读取,并返回 false。因此,在实际应用中,我们需要根据返回值来判断是否成功读取到了一行内容。