What are the potential pitfalls of using the ungreedy modifier "U" in preg_replace and how can they be avoided?

Using the "U" modifier in preg_replace can lead to unexpected results when dealing with patterns that contain nested quantifiers. To avoid this pitfall, it is recommended to use a more specific pattern that does not rely on the "U" modifier.

// Example of avoiding pitfalls with the "U" modifier in preg_replace
$pattern = '/<.*?>/U'; // Pattern using the "U" modifier
$replacement = ''; // Replacement string
$string = '<div>Example</div>'; // Input string

// Instead of using the "U" modifier, use a more specific pattern
$pattern = '/<[^>]*>/'; // Pattern without using the "U" modifier

$result = preg_replace($pattern, $replacement, $string);
echo $result; // Output: Example