php 如何在php中逐行读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6073235/
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 23:16:52  来源:igfitidea点击:

How to read line by line in php

php

提问by Technupe

when i try to insert each line into my oracle database i get an error stating invalid number, but if have only one line in the file it works fine.

当我尝试将每一行插入到我的 oracle 数据库中时,我收到一条错误消息,指出无效数字,但如果文件中只有一行,它就可以正常工作。

$file = @fopen('file.text', "r") ;  


// while there is another line to read in the file
while (!feof($file))
{
    // Get the current line that the file is reading
    $currentLine = fgets($file) ;
    $currentLine = explode('        ',$currentLine) ;
    insert($currentLine) ;


}   

fclose($file) ;

the lines look like this

线条看起来像这样

1     4     100
1     4     101

采纳答案by Marc B

Your explode()call is using 9 spaces as a separator, but your file only appears to have 5 spaces between each number. This means your $currentlinebe a single element array containing the original line, and not separate elements with a number in each.

您的explode()呼叫使用 9 个空格作为分隔符,但您的文件在每个数字之间似乎只有 5 个空格。这意味着您$currentline是一个包含原始行的单个元素数组,而不是每个元素都带有数字的单独元素。

Either change the number of spaces in the explode call, or change to

更改爆炸调用中的空格数,或更改为

$currentLine = preg_split('/\s+/', $currentLine);

which will split on any number of sequential spaces.

这将在任意数量的顺序空间上拆分。

回答by Fosco

Try this:

尝试这个:

$currentLine = trim(fgets($file)); 

It's possibly failing on the newline/carriage-return at the end of the line.

它可能在行尾的换行符/回车符上失败。

If not, where is this insert() function defined? Build a debug section that echos or writes out the attempted queries so you can really see the issue.

如果没有,这个 insert() 函数在哪里定义?构建一个调试部分来回显或写出尝试的查询,以便您可以真正看到问题。

回答by Jonathan Kuhn

I would double check that there is not a new line at the end of the file. Maybe even double check inside of php that the line read is not blank.

我会仔细检查文件末尾是否没有新行。甚至可能在 php 内部仔细检查读取的行不是空白。

$lineCount=0;
// while there is another line to read in the file
while (!feof($file))
{
    $lineCount++;
    // Get the current line that the file is reading
    $currentLine = fgets($file);
    if(trim($currentLine) != ''){
        $currentLine = explode('        ',$currentLine) ;
        insert($currentLine) ;
        echo "Inserted line number $lineCount<br />";
    } else {
        echo "There was a blank line at line number $lineCount.<br />";
    }
} 

回答by inoujk

try this :

尝试这个 :

// Read all data in the file
$file_all = file_get_contents('file.text') or die("unable to open file");
// create an array with each index = a line
$file_lines = explode(chr(13),$file_all);
// Insert each line found
foreach ($file_lines as $file_line) { insert($file_line); };

回答by Cobra_Fast

$lines = explode("\n", str_replace("\r", "", file_get_contents("file.text")));

foreach ($lines as $line)
{
     insert(explode('        ', $line);
}