Quick URL Validation in PHP

In this tutorial I’ll show you how to quickly validate URL in php , In php there is pre defined function called filter_var. You can use this pre defined function to validate URL in just one line of code, That will help you to insert valid URL in your database quickly using server side validation in PHP.



Here you just need to pass FILTER_VALIDATE_URL parameter in filter_var and It’ll validate URL/hostname/domain name quickly, See example below.

$url = "http://www.iamrohit.in";
echo $chkUrl = (!filter_var($url, FILTER_VALIDATE_URL) === false) ? 'Valid' : 'Invalid';
//OutPut: Valid
$url = "http//www.iamrohit.in";
echo $chkUrl = (!filter_var($url, FILTER_VALIDATE_URL) === false) ? 'Valid' : 'Invalid';
//OutPut: Invalid


Posted in: PHP

5 Replies to “Quick URL Validation in PHP”

  1. hello rohit,
    your all post are really very helpful and easy to understand also.
    can you give one article for
    wht is api in php. how it works.how many types api. how many ways we can call an api?
    soap rest api.
    what is web servies? how we make in php. please

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.