How can preg_replace_callback be utilized to handle multiple URLs in a string in PHP?

When dealing with multiple URLs in a string in PHP, preg_replace_callback can be utilized to handle each URL individually. This function allows you to perform a regular expression search and replace with a callback function for each match found in the string. By using preg_replace_callback, you can dynamically process each URL separately and apply any necessary transformations or replacements.

$string = "Check out my website at https://www.example1.com and also visit https://www.example2.com for more information.";

$updated_string = preg_replace_callback(
    '/https?:\/\/\S+/',
    function($match) {
        $url = $match[0];
        // Perform any necessary transformations on the URL here
        return "<a href='$url'>$url</a>";
    },
    $string
);

echo $updated_string;