What are the potential pitfalls of using preg_match_all with multiple occurrences of a BBCode-like pattern in PHP?

Using preg_match_all with multiple occurrences of a BBCode-like pattern in PHP can lead to unexpected results if the pattern contains nested tags or if the tags are not properly closed. To solve this issue, you can use a recursive regular expression pattern to match nested tags and ensure proper closure of the BBCode-like pattern.

<?php

// Example of using preg_match_all with a recursive regular expression pattern to match nested BBCode-like tags
$text = "[b]Bold text [i]italic and [u]underlined[/u][/i][/b]";
$pattern = '/\[(\w+)(?:=(.*?)|)\](?:(?R)|.*?)\[\/\1\]/s';
preg_match_all($pattern, $text, $matches);

print_r($matches[0]);

?>