What potential pitfalls should be considered when using preg_replace to highlight URLs in PHP?

When using preg_replace to highlight URLs in PHP, one potential pitfall to consider is that the regular expression used may not accurately match all possible URL formats, leading to some URLs not being highlighted. To solve this, it's important to use a robust regular expression pattern that covers various URL formats.

// Example code snippet using a more comprehensive regular expression pattern to highlight URLs
$text = "Check out my website at https://www.example.com and my blog at www.blog.com";
$pattern = '/(https?:\/\/[^\s]+)/';
$replacement = '<span style="color: blue;">$1</span>';
$highlighted_text = preg_replace($pattern, $replacement, $text);
echo $highlighted_text;