In what scenarios would using a callback function with preg_replace be more advantageous than using str_replace in PHP?
Using a callback function with preg_replace in PHP can be more advantageous than using str_replace when you need to perform more complex replacements that involve patterns or conditions. Callback functions allow you to dynamically determine the replacement based on the matched pattern, providing more flexibility and control over the replacement process.
// Example of using preg_replace with a callback function to replace words with their uppercase versions
$string = "hello world";
$pattern = '/\b\w+\b/';
$result = preg_replace($pattern, function($match) {
return strtoupper($match[0]);
}, $string);
echo $result; // Output: HELLO WORLD