What is the common issue with using preg_replace() to modify URLs in PHP?

The common issue with using preg_replace() to modify URLs in PHP is that it may not handle all possible variations of URLs correctly, leading to unexpected results or errors. To solve this issue, it is recommended to use the built-in PHP function filter_var() with the FILTER_VALIDATE_URL filter to validate and sanitize URLs before processing them.

$url = "http://example.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
    // URL is valid, proceed with processing
    // Your code here
} else {
    // URL is not valid, handle error accordingly
    echo "Invalid URL";
}