3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = "{tab=Text1} <p> nejaky</p><p>dlouhy text 1</p> ... {tab=Text2} <p>Jiny text...{test}.</p> {tab=Text3} .... {/tab} {tab=Text4} <p> nejaky</p><p>dlouhy text 1</p> ... "; $pattern="/(?:{tab=(?P<tab>[^}]+)})\n(?P<content>(?:(?!\n{\/?tab[^}]*}).)*)/s"; preg_match_all($pattern, $text, $matches); $result = []; if($matches) foreach($matches["tab"] as $i => $name) { if(isset($result[$name])) throw new Exception("Duplicate tab name '{$name}'"); $result[$name] = $matches["content"][$i]; } var_dump($result);

preferences:
48.9 ms | 402 KiB | 5 Q