<?php $bb = '[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]'; // regex with start, paragraph, and end capture groups $regex = '#(?<start>(\[[a-z]*\])*+)(?<paragraph>.*)(?<end>(\[\/[a-z]*\])*+)#U'; // put matches into $matches array preg_match_all($regex, $bb, $matches); // get the stuff we need $start = $matches['start'][0]; // string(12) "[i][b][i][b]" $paragraph = implode('', $matches['paragraph']); // now we will grab each tag $regex = '#\[(?<tag>[a-z])\]#'; preg_match_all($regex, $start, $matches); $tags = array_unique($matches['tag']); // and build up the new string $newString = ''; foreach($tags as $tag) { $newString .= '[' . $tag . ']'; } // create the end tags $end = str_replace('[', '[/', $newString); // put it all together $newString .= $paragraph . $end; echo $newString; // [i][b](This is a paragraph with BBcode.)[/i][/b]
You have javascript disabled. You will not be able to edit any code.