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 second 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.
In your code you’ve read the line in
if()and if it matches the condition you asked php to read one more lineps: regular expressions is an overengineering here,
striposis enough to check if the line starts with some substring.