How to use filter_var( ) Function In PHP

To validate a URL, the first approach is to use the filter_var( ) method with the FILTER_VALIDATE_URL filter. This method is widely used in PHP to filter a variable with a specific filter. It ensures that URLs are correctly formatted and secure, thus reducing the chances of security vulnerabilities.

Example: Implementation to validate URL in PHP Forms using filter_var( ).

PHP




<?php
   
// Taking a sample URL
$url = "https://www.example.in";
 
// Checking if the URL is valid or not
if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "$url is a VALID URL";
} else {
    echo "$url is a INVALID URL";
}
?>


Output

https://www.example.in is a VALID URL






PHP Forms Validate URL

Form validation is a necessary process that must be done before submitting the data to the database. Any time a user submits a URL, it becomes crucial to investigate whether the given URL is valid or not. We can use PHP to validate the URLs that are submitted through an HTML form.

Table of Content

  • Using filter_var( ) Function
  • Using preg_match( ) Function

Similar Reads

Using filter_var( ) Function

To validate a URL, the first approach is to use the filter_var( ) method with the FILTER_VALIDATE_URL filter. This method is widely used in PHP to filter a variable with a specific filter. It ensures that URLs are correctly formatted and secure, thus reducing the chances of security vulnerabilities....

Using preg_match( ) Function

...