Header-Ad

Linux & Web Development Geek - Tips & Tricks

PHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems.

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().


How to validate a URL in PHP


 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($urlFILTER_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.! ♥