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));
2
  • see this: stackoverflow.com/questions/5775452/… Commented Feb 20, 2014 at 3: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" Commented Feb 20, 2014 at 3:59

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.