There is a log log.txt form:

 -------- текст array{....} текст текст -------- array{....} текст 

How to parse arrays from this log? That is, the output was:

 array{....} array{....} 

I try this:

 $s = file_get_contents('log.txt'); $arr = explode("----------",$s); foreach ($arr as $key => $val){ $array = split('array', $val); echo "<pre>"; echo $array; echo "</pre>"; } 

But it cuts only the word array, but I need the contents

  • And what have you tried? - teo van kot
  • Added to the topic what I'm doing - Nikolay

2 answers 2

You can select the necessary lines as follows:

 $arrays = preg_grep('/\s+array{/', file('log.txt')); 
  • Why is it that here and in the lower example it displays an empty array, what could be wrong? - Nikolay
  • make var_export(file('log.txt')); and post a slice - splash58
  • I, too, probably have not woken up yet! Everything is in order, I just pointed out curly brackets instead - Nikolay
  • happens :) so be it nice - splash58
  • And then the brace should not be closed in a pattern? - Nikolay

You are welcome:

 $s = file_get_contents('log.txt'); // Работает в PHP 5.2.2 и старше. preg_match_all('/array{.+}/', $s, $matches); print_r($matches); 

Sample online .

  • at least add g after the pattern - splash58
  • It's probably better to just use preg_match_all , thanks - teo van kot