How can variables be used to define functions in PHP for parsing BBCode?

To define functions for parsing BBCode in PHP, variables can be used to store the BBCode tags and their corresponding HTML replacements. By using variables to store this information, the code becomes more modular and easier to maintain. This allows for easy customization and expansion of the BBCode parsing functionality.

// Define variables for BBCode tags and their HTML replacements
$bbcode_tags = array(
    '[b]' => '<strong>',
    '[/b]' => '</strong>',
    '[i]' => '<em>',
    '[/i]' => '</em>',
    '[u]' => '<u>',
    '[/u]' => '</u>'
);

// Function to parse BBCode
function parseBBCode($text) {
    global $bbcode_tags;
    
    foreach($bbcode_tags as $bbcode => $html) {
        $text = str_replace($bbcode, $html, $text);
    }
    
    return $text;
}

// Example usage
$bbcode_text = '[b]Bold[/b] [i]Italic[/i] [u]Underline[/u]';
echo parseBBCode($bbcode_text);