Search code examples
phpregexbbcode

How to clean duplicate BB tags


[i][b][i][b](This is a paragraph with BBcode.)[/b][/i][/b][/i]

Some of my BB code has double tags, whats the best way to remove this?

I've tried a few things mostly regex, but I'm honestly a novice when it comes to regex.


Solution

  • This is absolutely horrible, but it works.

    <?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]
    

    Which gives you [i][b](This is a paragraph with BBcode.)[/i][/b]

    Check it here https://3v4l.org/O8UHO