What is the significance of a unique uid for each bb-tag in PHPBB when using a bb-code replacer?

When using a bb-code replacer in PHPBB, having a unique uid for each bb-tag is important to ensure that the replacement is applied correctly to each instance of the bb-tag. This prevents unintended replacements or errors in the output. By assigning a unique uid to each bb-tag, you can accurately target and replace specific instances of the bb-tag without affecting others.

// Example of assigning a unique uid to each bb-tag in a PHPBB bb-code replacer

// Define an array to store unique uids for each bb-tag
$uid_counter = [];

// Function to replace bb-tags with unique uids
function replace_bb_tags($text) {
    global $uid_counter;
    
    // Replace [bb-tag] with [uid-1], [bb-tag] with [uid-2], etc.
    $text = preg_replace_callback('/\[bb-tag\]/', function($matches) use (&$uid_counter) {
        $uid = isset($uid_counter['bb-tag']) ? $uid_counter['bb-tag'] + 1 : 1;
        $uid_counter['bb-tag'] = $uid;
        return '[uid-' . $uid . ']';
    }, $text);
    
    return $text;
}

// Example usage
$text = "This is a [bb-tag]example[/bb-tag] of a bb-tag.";
$text = replace_bb_tags($text);
echo $text;