PHP reading first line of each file. *output overwrites the previous line*
I'm trying to read a text file line-by line. I added a condition where if the first word is not "insert", skip it, but if "insert is the first word", print it out.
Here is the code:
$matching = "^insert";
$file = fopen("test.sql.text", "r") or exit("Unable to open file!");
while(!feof($file))
{
if(eregi($matching, fgets($file)) ){
echo fgets($file)."<br />";
} else
echo "invalid beginning value <br />";
}
fclose($file);
I have two lines in a file both beginning with "insert". For some reason, it seems to over write the first insert with the sec开发者_如何学编程ond insert. I would like to be able to print out any line that begins with an insert.
Any help or advice would be very appreciated!
Thank you.
$line = fgets($file);
if(stripos($line, 'insert') === 0){
echo $line . '<br />';
} else {
echo 'invalid beginning value <br />';
}
In your code you've read the line in if()
and if it matches the condition you asked php to read one more line
ps: regular expressions is an overengineering here, stripos
is enough to check if the line starts with some substring.
精彩评论