<?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); }
You have javascript disabled. You will not be able to edit any code.