How can PHP be used to extract URLs from BB-Code tags within text?

To extract URLs from BB-Code tags within text using PHP, you can use regular expressions to search for the URLs within the BB-Code tags and extract them. By using preg_match_all function with a suitable regular expression pattern, you can easily retrieve the URLs contained within the BB-Code tags.

$text = "[url]https://example.com[/url] Some text [url]https://example2.com[/url]";
$pattern = '/\[url\](.*?)\[\/url\]/';
preg_match_all($pattern, $text, $matches);

$urls = $matches[1];
foreach($urls as $url) {
    echo $url . "\n";
}