What are the best practices for utilizing BBCode in PHP to enable specific formatting options?

When working with BBCode in PHP to enable specific formatting options, it is important to sanitize the input to prevent any malicious code injection. One way to do this is by using the `htmlspecialchars()` function to escape special characters. Additionally, you can create a function to parse and replace BBCode tags with their corresponding HTML tags for formatting.

function parseBBCode($text) {
    $bbcode = array(
        '/\[b\](.*?)\[\/b\]/is' => '<strong>$1</strong>',
        '/\[i\](.*?)\[\/i\]/is' => '<em>$1</em>',
        '/\[u\](.*?)\[\/u\]/is' => '<u>$1</u>'
    );

    foreach ($bbcode as $pattern => $replacement) {
        $text = preg_replace($pattern, $replacement, $text);
    }

    return $text;
}

$input = "[b]This[/b] is [i]formatted[/i] text.";
echo parseBBCode($input);