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