0

I have a file which contain data like this -

keysuccess
line1
line2
keyerror
line3
line4
line5
keyfail
line6
line7

From here I would like to extract the line3, line4, and line5 which is the group of keyerror

something like -

$array = explode("keyerror", file_get_contents($file));
newcomer
  • 467
  • 6
  • 18
  • see this: http://stackoverflow.com/questions/5775452/php-read-specific-line-from-file – timgavin Feb 20 '14 at 03:45
  • @Tim thanks for your reply but my file contain is dynamic. Somethime the keysuccess contain only line 1 and keyerror contain 5 line. So I want to get all the line in an array starting from keyerror to next word which start by "key" – newcomer Feb 20 '14 at 03:59

1 Answers1

1

The only way i can think with that specific file structure is by using file(), assuming the keys starts from keyX

$rows = file($file,FILE_IGNORE_NEW_LINES);
$found = FALSE;
$lines = array();
foreach($rows as $key => $row) {
   if ($found && substr($row,0,3)=="key") {
      break;
   }
   if ($found) {
      $lines[] = $row;
   }
   if ($row === "keyerror") {
      $found = TRUE;
   }
}

print_r($lines);
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141