Mega Code Archive

 
Categories / Php / Strings
 

Finding All Matching Lines in a File

<?php  $file = fopen("testfile.txt", "r") or die("Cannot open file!\n");  while ($line = fgets($file, 1024)) {      if (preg_match("/Hello( World!)?/", $line)) {          echo "Found match: " . $line;      } else {          echo "No match: " . $line;      }  }  fclose($file);  ?>