当我尝试将每一行插入到我的 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) ;

线条看起来像这样
1     4     100
1     4     101

最佳答案

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

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

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

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

关于php - 如何在php中逐行读取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6073235/

10-16 14:43