What potential issues can arise when using preg_match for URL validation?

One potential issue when using preg_match for URL validation is that the regular expression may not accurately capture all valid URL formats, leading to false negatives. To solve this, it's recommended to use the filter_var function with the FILTER_VALIDATE_URL filter, which provides a more robust URL validation.

$url = "https://www.example.com";

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Valid URL";
} else {
    echo "Invalid URL";
}