FILTER_VALIDATE_URL constant in PHP


The FILTER_VALIDATE_URL constant validates a URL.

Flags

  • FILTER_FLAG_SCHEME_REQUIRED − URL must be RFC compliant.

  • FILTER_FLAG_HOST_REQUIRED − URL must include host name.

  • FILTER_FLAG_PATH_REQUIRED −URL must have a path after the domain name.

  • FILTER_FLAG_QUERY_REQUIRED −URL must have a query string.

Return

The FILTER_VALIDATE_URL constant does not return anything.

Example

 Live Demo

<?php
$url = "https://www.example.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
   echo("Valid URL!");
} else {
   echo("Invalid URL!");
}
?>

Output

The following is the output.

Valid URL!

Let us see another example.

Example

 Live Demo

<?php
$url = "examplecom";
if (filter_var($url, FILTER_VALIDATE_URL)) {
   echo("Valid URL!");
} else {
   echo("Invalid URL!");
}
?>

Output

Here is the output.

Invalid URL!

Updated on: 27-Jun-2020

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements