3

我试图弄清楚如何在使用fgets(). 第一行可以用 解决if(!$firstLine),但我不确定如何忽略最后一行,或者我的忽略第一行的解决方案是否是最佳选择。

4

5 回答 5

9
fgets($file); //Ignore the first line
$line = fgets($file);
$next = fgets($file); 
while ($next !== false) { //check the line after the one you will process next.  
                //This way, when $next is false, then you still have one line left you could process, the last line.
    //Do Stuff
    $line = $next;
    $next = fgets($file);
}
于 2012-11-30T00:19:56.647 回答
0
$lines = file('http://www.example.com/');//or local file

unset($lines[0]);
unset($lines[count($lines)-1)]);
$new_file = implode('', $lines); //may want to use line break "\n" for the join
于 2012-11-30T00:21:24.177 回答
0

您可能想尝试分解文件内容。它会帮助你很多!之后,您将对其进行标记。

于 2012-12-04T17:37:48.987 回答
-1

不幸的是, file() 会将文件打开到“串行”流中(不确定串行是否正确),这意味着您必须在检测到他的结尾之前读取完整的文件

于 2012-11-30T00:16:29.913 回答
-1

试试这个

 $str= file_get_contents("file.txt");
 $arr = preg_split ("\n",$str);
 $n = count($arr);

$n 是行数

$str="";
for($i=0;$i<$n;$i++){
   if($i>0 && $i<($n-1)){ $str.=$arr[$i]}
   }
    /**Str without line 1 and final line */
于 2012-11-30T00:26:03.933 回答