Skip to content
PHP Logo

Are there any built-in PHP functions or extensions specifically designed for handling BB-Codes?

When working with BB-Codes in PHP, there are no built-in functions or extensions specifically designed for handling them. However, you can create your own functions to parse and convert BB-Codes to HTML or vice versa. One common approach is to use regular expressions to identify and replace BB-Codes with their corresponding HTML tags.

php
function parseBBCode($text) {
    // Define an array of BB-Codes and their corresponding HTML tags
    $bbCodes = array(
        '[b]' => '<strong>',
        '[/b]' => '</strong>',
        '[i]' => '<em>',
        '[/i]' => '</em>',
        // Add more BB-Codes as needed
    );

    // Replace each BB-Code with its corresponding HTML tag
    foreach ($bbCodes as $bbCode => $htmlTag) {
        $text = str_replace($bbCode, $htmlTag, $text);
    }

    return $text;
}

// Example usage
$bbText = '[b]Hello[/b] [i]World[/i]';
$htmlText = parseBBCode($bbText);
echo $htmlText; // Output: <strong>Hello</strong> <em>World</em>