<?php $string = '[Привет/Здравствуй], [Артём/Миша/Саша/Паша/Даша/Петя/И другие], [Гулять/Пить/Курить]'; preg_match_all('/\[(.*?)\]/i', $string, $match); $action = explode('/', $match[1][0]); $names = explode('/', $match[1][1]); recursion($match, $element, 0, count($match[0]) - 1); function recursion($match, $element, $level, $max_level) { $data = explode('/', $match[1][$level]); for ($i = 0; $i < count($data); $i++) { $element[$level] = $data[$i]; if ($level !== $max_level) { recursion($match, $element, $level + 1, $max_level); } else { echo(implode(', ', $element) . '<br>'); } } } ?>