3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = 'Hello how [t- are] you [k- today], Sir?'; $types = ['' => 'normal', 't' => 'target', 'k' => 'key']; if ( preg_match_all('~ (?| \[ (?<type>[^]-]+) - \h (?<content>[^]]+) ] | () ([^[]+) ) ~x', $str, $matches, PREG_SET_ORDER) ) { foreach ($matches as &$m) { unset($m[0], $m[1], $m[2]); $m['type'] = $types[$m['type']]; } print_r($matches); }

preferences:
17.28 ms | 405 KiB | 5 Q