wwfc_barmy_army Posted April 17, 2010 Share Posted April 17, 2010 Hello, I have this code: $file = fopen("myfile","r"); while(! feof($file)) { if( preg_match( "/^Number\b/", fgets($file) )) { echo fgets($file); } } fclose($file); I want it to print the line that starts with 'Number' but for some reason it is printing the line AFTER the line that starts with 'Number' Anyone got any suggestions? Thanks. Link to comment https://forums.phpfreaks.com/topic/198872-reading-the-right-line-from-a-file-using-fopen/ Share on other sites More sharing options...
salathe Posted April 17, 2010 Share Posted April 17, 2010 Of course, the call to fgets used when calling preg_match reads the line that you are really interested in. Then when you call fgets again, it continues from where it left off and reads the following line. You probably want something like: while( ! feof($file)) { $line = fgets($file); if (preg_match("/^Number\b/", $line)) { echo $line; } } Link to comment https://forums.phpfreaks.com/topic/198872-reading-the-right-line-from-a-file-using-fopen/#findComment-1043897 Share on other sites More sharing options...
wwfc_barmy_army Posted April 17, 2010 Author Share Posted April 17, 2010 Ah, how did I miss that. Thanks salathe. Link to comment https://forums.phpfreaks.com/topic/198872-reading-the-right-line-from-a-file-using-fopen/#findComment-1043908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.