Skip to content

Commit c60330b

Browse files
committed
feat: add new method readFirstLine()
1 parent dc6efd0 commit c60330b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Traits/FileSnippetReadTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use function count;
1919
use function fclose;
2020
use function feof;
21+
use function fgets;
22+
use function fopen;
2123
use function fseek;
2224

2325
/**
@@ -137,6 +139,22 @@ public static function getLines5u3d(string $file, int $baseLine = 1): array
137139
return self::readRangeLines($file, $baseLine, 5);
138140
}
139141

142+
/**
143+
* Read the first line contents
144+
*
145+
* @param string $filepath
146+
*
147+
* @return string
148+
*/
149+
public static function readFirstLine(string $filepath): string
150+
{
151+
$file = fopen($filepath, 'rb');
152+
$line = fgets($file);
153+
fclose($file);
154+
155+
return (string)$line;
156+
}
157+
140158
/**
141159
* 读取文件的最后几行(支持大文件读取)
142160
*

0 commit comments

Comments
 (0)