What role does the "U" modifier play in PHP regular expressions when dealing with pattern matching and replacements?

The "U" modifier in PHP regular expressions changes the default greedy behavior of quantifiers to be ungreedy. This means that the quantifiers will match as few characters as possible instead of as many as possible. This can be useful when dealing with patterns that contain optional or repeating elements that you want to match in a non-greedy way.

$pattern = '/<.*?>/U'; // Using the "U" modifier to make the quantifier ungreedy
$string = '<html><body><h1>Hello World</h1></body></html>';
preg_match($pattern, $string, $matches);
echo $matches[0]; // Output: <html>