3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "some text here [baz] and here [you|him|her|foo|bar|baz|foo|option|test] and even here [another]."; $pattern = (function () use ($string) { $array = []; for ($i = 0; $i <= substr_count($string, "|"); $i++) { $array[] = $i == 0 ? '([^][|]+)' : '([^][|]+)?'; } return implode("\|?", $array); })(); preg_match_all("~\[$pattern]~", $string, $matches, PREG_SET_ORDER); var_dump($matches);

preferences:
25.67 ms | 407 KiB | 5 Q