Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sanitize and Validate a URL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 179
    Comment on it

    To sanitize and validate a URL a function filter_var() is used. It also removes all the illegal characters from the URL. It includes two parameters one is the variable that is needed to check and other is the type of the variable. If you want to remove all the illegal characters from a URL the value of type is FILTER_SANITIZE_URL.

    For example:

    filter_var($url, FILTER_SANITIZE_URL);
    

    If you want to validate the URL then the type value is FILTER_VALIDATE_URL. For example:

    filter_var($url, FILTER_VALIDATE_URL)
    

    The above function returns true if the $url value is valid else it returns false.

    To sanitize and validate a URL example is given below:

    <?php
    $url = "http://www.w3schools.com";
    
    // Remove all illegal characters from a url
    $url = filter_var($url, FILTER_SANITIZE_URL);
    
    // Validate url
    if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
        echo("$url is a valid URL");
    } else {
        echo("$url is not a valid URL");
    }
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: