cod*_*ict 651
您可以使用该fgets()函数逐行读取文件:
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
} else {
// error opening the file.
}
Run Code Online (Sandbox Code Playgroud)
小智 125
if ($file = fopen("file.txt", "r")) {
while(!feof($file)) {
$line = fgets($file);
# do same stuff with the $line
}
fclose($file);
}
Run Code Online (Sandbox Code Playgroud)
els*_*hll 82
您可以为文件使用面向对象的接口类 - SplFileObject http://php.net/manual/en/splfileobject.fgets.php(PHP 5> = 5.1.0)
<?php
$file = new SplFileObject("file.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;
Run Code Online (Sandbox Code Playgroud)
Nin*_*pac 53
如果您要打开一个大文件,您可能希望使用生成器和fgets()来避免将整个文件加载到内存中:
/**
* @return Generator
*/
$fileData = function() {
$file = fopen(__DIR__ . '/file.txt', 'r');
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
yield $line;
}
fclose($file);
};
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
foreach ($fileData() as $line) {
// $line contains current line
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以在foreach()中处理单个文件行.
注意:生成器要求> = PHP 5.5
Sta*_*arx 28
使用缓冲技术来读取文件.
$filename = "test.txt";
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer = fread($source_file, 4096); // use a buffer of 4KB
$buffer = str_replace($old,$new,$buffer);
///
}
Run Code Online (Sandbox Code Playgroud)
NoI*_*Guy 28
有一个file()函数返回文件中包含的行数组.
foreach(file('myfile.txt') as $line) {
echo $line. "\n";
}
Run Code Online (Sandbox Code Playgroud)
Quo*_*ons 17
foreach (new SplFileObject(__FILE__) as $line) {
echo $line;
}
Run Code Online (Sandbox Code Playgroud)
SplFileObject 在处理大文件时很有用。
function parse_file($filename)
{
try {
$file = new SplFileObject($filename);
} catch (LogicException $exception) {
die('SplFileObject : '.$exception->getMessage());
}
while ($file->valid()) {
$line = $file->fgets();
//do something with $line
}
//don't forget to free the file handle.
$file = null;
}
Run Code Online (Sandbox Code Playgroud)
所有答复都没有明显的答案.PHP有一个整洁的流分隔符解析器,可用于此目的.
$fp=fopen("/path/to/the/file", "r+");
while ($line = stream_get_line($fp, 1024 * 1024, "\n"))
{
echo $line;
}
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
小心'while(!feof ... fgets()'的东西,fgets可以得到一个错误(returnfing false)并永远循环而不会到达文件的末尾.codaddict最接近正确但是当你的'while fgets'时循环结束,检查feof;如果不是,则出现错误.
这个问题的流行解决方案之一将涉及新线字符的问题.简单就可以很容易地修复它str_replace.
$handle = fopen("some_file.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = str_replace("\n", "", $line);
}
fclose($handle);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这就是我如何处理非常大的文件(测试高达100G)的方式。它比fgets()更快
$block =1024*1024;//1MB or counld be any higher than HDD block_size*2
if ($fh = fopen("file.txt", "r")) {
$left='';
while (!feof($fh)) {// read the file
$temp = fread($fh, $block);
$fgetslines = explode("\n",$temp);
$fgetslines[0]=$left.$fgetslines[0];
if(!feof($fh) )$left = array_pop($lines);
foreach ($fgetslines as $k => $line) {
//do smth with $line
}
}
}
fclose($fh);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
613625 次 |
| 最近记录: |