<?php
function getFileLines($filename, $startLine = 1, $endLine = 50, $method = 'rb'){
$content = array();
if (version_compare(PHP_VERSION, '5.1.0', '>=')){ // 5.1版本之上使用Splfileobject
$count = $endLine - $startLine;
$fp = new SplFileObject($filename, $method);
$fp -> seek($startLine - 1); // 转到$startLine行,seek方法从0开始计数
for ($i = 0; $i <= $count; ++$i){
$content[] = trim(($fp -> current()), "\n"); //current()方法取当前行清除换行保存到数组
$fp -> next(); //循环下一行
}
}else{ // PHP<5.1
$fp = fopen($filename, $method);
if (!$fp)
return 'error:can not read file';
for ($i = 1; $i < $startLine; ++$i){ //定位到$startLine行
fgets($fp);
}
for ($i; $i <= $endLine; ++$i){
$content[] = fgets($fp); //从$startLine行开始循环读取到$endLine行
}
fclose($fp);
}
return array_filter($content); //返回array_filter(flase,null,0)过滤后的数组
}
?>
示例:
<?php
$filename = 'file.txt';
$start = 1;
$end = 10;
$count = $end - $start;
while (true){
$data = getFileLines($filename, $start, $end);
// var_dump($data);
$rev = ver($data);
if($data[9] == ''){
die("End");
sleep(10);
}
$start = $start + 10;
$end = $end + 10;
}
?>
标签:php