我有一个具有更改日志的txt文件。我试图仅显示当前版本的新更改。如何开始使用PHP从指定行读取txt文件?
我写了一个函数来读取文件,并检查每一行是否有想要的单词,如果它发现这些单词开始获取内容并将其推送到数组。
我搜索了一下,看看是否有例子,但是大家都在谈论如何停在指定的行,而不是从一个开始。
这里是我使用的代码:
public function load($theFile, $beginPosition, $doubleCheck) {
// Open file (read-only)
$file = fopen($_SERVER['DOCUMENT_ROOT'] . '/home/' . $theFile, 'r');
// Exit the function if the the file can't be opened
if (!$file) {
return;
}
$changes = Array();
// While not at the End Of File
while (!feof($file)) {
// Read current line only
$line = fgets($file);
// This will check if the current line has the word we look for to start loading
$findBeginning = strpos($line, $beginPosition);
// Double check for the beginning
$beginningCheck = strpos($line, $doubleCheck);
// Once you find the beginning
if ($findBeginning !== false && $beginningCheck !== false) {
// Start storing the data to an array
while (!feof($file)) {
$line = fgets($file);
// Remove space and the first 2 charecters ('-' + one space)
$line = trim(substr($line, 2));
if (!empty($line)) { // Don't add empty lines
array_push($changes, $line);
}
}
}
}
// Close the file to save resourses
fclose($file);
return $changes;
}
它的工作现在,但你可以看到它的嵌套循环,这就是不好的,万一txt文件的增长将需要更多的时间!
我试图改善性能,那么有没有更好的方法来做到这一点?
真的很棒,更简单的代码,并诀窍:D 我尝试使用'file()'函数,但我错了! 我注意到它需要更多的时间,但没关系。我喜欢这种方法。谢谢! – Maher4Ever 2010-08-11 22:36:52
请注意,借此,您可以将整个文件有效地读入内存。适用于小文件..但如果文件太大,最终可能会导致脚本死亡。特别是在繁忙的服务器上。 – cHao 2012-09-21 18:50:44