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);

preferences:
45.59 ms | 1551 KiB | 5 Q