How to use FILTER VALIDATE URL in PHP

This article describe about FILTER_VALIDATE_URL Filter in PHP.
  • 2622

FILTER_VALIDATE_URL Filter

For filter validates value as an URL use FILTER_VALIDATE_URL filter.

  • Name: "validate_url"
  • ID-number: 273

Possible flags:

FILTER_FLAG_SCHEME_REQUIRED - It take URL to be an RFC URL (like http://program)
FILTER_FLAG_HOST_REQUIRED - It take URL to include host name (like http://www.program.com)
FILTER_FLAG_PATH_REQUIRED - It take URL to have a path after the domain name (like www.program.com/program1/test2/)
FILTER_FLAG_QUERY_REQUIRED - It take URL to have a query string (like "program.php?name=Raman&age=27")

Example 1

<html>
<body>
<?php
$url = "http://www.program.com";
if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "URL is not valid";
}
else
{
echo "URL is valid";
}
?>
</body>
</html>

 

Output


filtervalidateurlphp1.jpg

 

Example 2

 

<html>
<body>
<?php
$url = "program.php?name=Raman&age=27";
if(!filter_var($url, FILTER_VALIDATE_URL,
FILTER_FLAG_QUERY_REQUIRED))
{
echo "URL is not valid";
}
else
{
echo "URL is valid";
}
?>
</body>
</html>

 

Output


filtervalidateurlphp2.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.