Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts
Saturday, October 6, 2012
How to Validate a URL in PHP? [Easy]
7:41 PM
Starting from PHP 5.2 we have bunch of built-in functions which makes it easier for programmers to perform small functions without writing up code for them,one of such function is filter_var(); , using filter_var().
We can validate emails and URL's easily with just a few steps or you can say only 4-5 lines of code, so lets say you want to validate a URL using filter_var() you can do it as follow:
To use this function simply use the following code :
if(checkurl("http://www.google.com")) {
echo 'URL is correct';
//continue....
}else{
echo 'URL is incorrect';
//through an error!
}
Simple isn't? And if you don't want to use the complete function,you can simply use the filter_var("Your URL here!", FILTER_VALIDATE_URL); .
Comments are welcome, and once more you must have PHP >= 5.2.
We can validate emails and URL's easily with just a few steps or you can say only 4-5 lines of code, so lets say you want to validate a URL using filter_var() you can do it as follow:
function checkurl($url)
return filter_var($url, FILTER_VALIDATE_URL);
}
To use this function simply use the following code :
if(checkurl("http://www.google.com")) {
echo 'URL is correct';
//continue....
}else{
echo 'URL is incorrect';
//through an error!
}
Simple isn't? And if you don't want to use the complete function,you can simply use the filter_var("Your URL here!", FILTER_VALIDATE_URL); .
Comments are welcome, and once more you must have PHP >= 5.2.
♥ Happy Coding.! ♥
Subscribe to:
Posts (Atom)