How can BBCode be implemented in PHP for formatting text?

To implement BBCode in PHP for formatting text, you can create a function that replaces BBCode tags with their corresponding HTML markup. This function can be used to parse text containing BBCode and output the formatted text. Here is a sample PHP code snippet that demonstrates how to implement BBCode functionality:

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

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

    return $text;
}

// Example usage
$bbcodeText = "[b]Bold[/b] [i]Italic[/i] [u]Underline[/u] [url=https://www.example.com]Link[/url]";
$htmlText = bbcode2html($bbcodeText);
echo $htmlText;