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));
有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top