You can use preg_split() .
Regex:
preg_split('@([$#])|[{}/] +@ ', $text)
And discard the first and third elements of the array.
ideone demo
EDIT: To reflect the new conditions set by OP in the comments (not in the question): it should check the syntax ^\{\$\w+#\w+(?:/\w*)$ and tokenize in var, module and parameters independently.
Regex:
~\G(?(?=^)\{\$(\w++)
Code:
// http://stackoverflow.com/q/32969465/5290909 $pattern = '@\G(?(?=^)\{\$(\w++)#(\w++)(?=[\w/]+}$))/\K\w* +@ '; $text = '{$gallery#pager/collectionName/imageName/manual/no_effect/foo1/foo2/fooN}'; $result = preg_match_all($pattern, $text, $matches); if ($result === 0) { // is invalid, does not match '~^\{\$\w+#\w+(?:/\w*)+$~' echo "Invalid text"; } else { // Assign vars (only to clarify) $module_name = array_pop($matches)[0]; $var_name = array_pop($matches)[0]; $parameters = $matches; echo "VAR NAME: \t'$var_name'\nMODULE: \t'$module_name'\nPARAMETERS:\n"; print_r($matches); }
Output:
VAR NAME: 'gallery' MODULE: 'pager' PARAMETERS: Array ( [0] => collectionName [1] => imageName [2] => manual [3] => no_effect [4] => foo1 [5] => foo2 [6] => fooN )
ideone demo
source share