如何在php中逐行读取 [英] How to read line by line in php
问题描述
当我尝试将每一行插入到oracle数据库时,我收到一条错误消息,指出无效数字,但是如果文件中只有一行,则可以正常工作.
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.
$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
是包含原始行的单个元素数组,而不是每个元素中都有数字的单独元素.
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 $currentline
be a single element array containing the original line, and not separate elements with a number in each.
要么更改爆炸调用中的空格数,要么更改为
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.
这篇关于如何在php中逐行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!