FILTER_VALIDATE_URL constant in PHP

PHPProgrammingServer Side Programming

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!
raja
Published on 04-Dec-2018 07:24:30
Advertisements