php 从文件中读取第一行的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4521936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:18:13  来源:igfitidea点击:

Quickest Way to Read First Line from File

phpfile

提问by Jonah

What's the quickest, easiest way to read the first line only from a file? I know you can use file, but in my case there's no point in wasting the time loading the whole file.

仅从文件中读取第一行的最快、最简单的方法是什么?我知道您可以使用file,但就我而言,浪费时间加载整个文件是没有意义的。

Preferably a one-liner.

最好是单层。

回答by ircmaxell

Well, you could do:

好吧,你可以这样做:

$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);

It's not one line, but if you made it one line you'd either be screwed for error checking, or be leaving resources open longer than you need them, so I'd say keep the multiple lines

这不是一行,但如果你把它写成一行,你要么被错误检查搞砸,要么让资源打开的时间比你需要的时间长,所以我会说保留多行

Edit

编辑

If you ABSOLUTELYknow the file exists, you can use a one-liner:

如果您绝对知道该文件存在,则可以使用单行:

$line = fgets(fopen($file, 'r'));

The reason is that PHP implements RAIIfor resources.

原因是 PHP为资源实现了RAII

That means that when the file handle goes out of scope (which happens immediately after the call to fgets in this case), it will be closed.

这意味着当文件句柄超出范围时(在这种情况下,在调用 fgets 之后立即发生),它将被关闭。

回答by profitphp

$firstline=`head -n1 filename.txt`;

回答by Sarah

$line = '';
$file = 'data.txt';
if($f = fopen($file, 'r')){
  $line = fgets($f); // read until first newline
  fclose($f);
}
echo $line;

回答by Tarjei Huse

I'm impressed no one mentioned the file() function:

没有人提到 file() 函数给我留下了深刻的印象:

$line = file($filename)[0];

or if version_compare(PHP_VERSION, "5.4.0") < 0:

或者如果 version_compare(PHP_VERSION, "5.4.0") < 0:

$line = array_shift(file($filename));

回答by Mateusz Bac?awski

if(file_exists($file)) {
    $line = fgets(fopen($file, 'r'));
}

回答by George SEDRA

fgets() returns " " which is a new line at the end,but using this code you will get first line without the lineBreak at the end :

fgets() 返回“”,这是末尾的新行,但使用此代码,您将获得第一行,末尾没有 lineBreak :

$handle = @fopen($filePath, "r");
$text=fread($handle,filesize($filePath));
$lines=explode(PHP_EOL,$text);
$line = reset($lines);

回答by Svetoslav Marinov

In one of my projects (qSandbox) I uses this approach to get the first line of a text file that I read anyways. I have my email templates are in a text files and the subject is in the first line.

在我的一个项目 (qSandbox) 中,我使用这种方法来获取我无论如何阅读的文本文件的第一行。我的电子邮件模板位于文本文件中,主题位于第一行。

$subj_regex = '#^\s*(.+)[\r\n]\s*#i';

// subject is the first line of the text file. Smart, eh?
if (preg_match($subj_regex, $buff, $matches)) {
    $subject = $matches[1];
    $buff = preg_replace($subj_regex, '', $buff); // rm subject from buff now.
}

回答by ngen

You could try to us freadand declare the file size to read.

您可以尝试我们fread并声明要读取的文件大小。

回答by rcourtna

If you don't mind reading in the entire file, then a one-liner would be:

如果您不介意阅读整个文件,那么单行将是:

$first_line = array_shift(array_values(preg_split('/\r\n|\r|\n/', file_get_contents($file_path), 2)));

:)

:)

回答by MisterFrogger

Try this:

尝试这个:

$file = 'data.txt';
$data = file_get_contents($file);
$lines = explode