How can PHP be used to automatically convert domains in bbcode?

To automatically convert domains in bbcode using PHP, you can use a regular expression to search for domain names within the bbcode content and replace them with the appropriate bbcode syntax for linking to a URL. This can be achieved by identifying URLs within the content and wrapping them with the [url] bbcode tags.

<?php
function convertDomainsToBBCode($content) {
    $pattern = '/(http|https):\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}(\S*)/';
    $replacement = '[url]$0[/url]';
    $content = preg_replace($pattern, $replacement, $content);
    
    return $content;
}

// Example usage
$bbcodeContent = "[url]http://www.example.com[/url] is a great website.";
$convertedContent = convertDomainsToBBCode($bbcodeContent);
echo $convertedContent;
?>