Google News
logo
PHP Program to Sanitize and validate a URL
In the following example of PHP program to sanitize and validate a URL using the `filter_var()` function :
Program :
<?php
$url = "https://www.freetimelearning.com";

// Sanitize the URL
$sanitized_url = filter_var($url, FILTER_SANITIZE_URL);

// Validate the sanitized URL
if (filter_var($sanitized_url, FILTER_VALIDATE_URL)) {
  echo "The URL is valid.";
} else {
  echo "The URL is not valid.";
}
?>
Output :
The URL is valid.
In the above program, the `FILTER_SANITIZE_URL` flag is used to sanitize the URL and remove any potentially malicious characters. Then, the `FILTER_VALIDATE_URL` flag is used to validate the sanitized URL.

If the URL is valid, the program will output "The URL is valid." Otherwise, it will output "The URL is not valid."