How can the preg_replace function be effectively replaced by preg_replace_callback in PHP scripts?

The preg_replace function in PHP is commonly used to perform pattern-based string replacements. However, when more complex replacements or logic is needed, the preg_replace_callback function can be used instead. This function allows you to define a callback function that will be executed for each match found in the input string, giving you more flexibility and control over the replacement process.

<?php
// Using preg_replace_callback instead of preg_replace
$input = "Hello, World!";
$pattern = "/\b(\w+)\b/";
$output = preg_replace_callback($pattern, function($matches) {
    return strtoupper($matches[0]);
}, $input);

echo $output; // Output: HELLO, WORLD!
?>