3v4l.org

run code in 500+ PHP versions simultaneously
<?php $text = '[HEAD] [TITLE-MAIN] example news1 [TITLE-SUB] title sub1 [CONTENT] content text1 [HEAD] [TITLE-MAIN] example news2 [TITLE-SUB] title sub2 [CONTENT] content text2'; $sections = array(); foreach (preg_split('/\s*\[HEAD\]\s*/', $text, -1, PREG_SPLIT_NO_EMPTY) as $section) { $s = trim($section); $tag_data = preg_split('/\[([^\]]+)\]/', $s, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); for ($i = 0; $i < count($tag_data); $i += 2) { $tags[$tag_data[$i]] = trim($tag_data[$i+1]); } $sections[] = $tags; } print_r($sections);
Output for 5.6.38, 7.1.0 - 7.1.23, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.31, 8.3.0 - 8.3.31, 8.4.1 - 8.4.22, 8.5.0 - 8.5.7
Array ( [0] => Array ( [TITLE-MAIN] => example news1 [TITLE-SUB] => title sub1 [CONTENT] => content text1 ) [1] => Array ( [TITLE-MAIN] => example news2 [TITLE-SUB] => title sub2 [CONTENT] => content text2 ) )

preferences:
111.83 ms | 1552 KiB | 4 Q