Change language

PHP Filter_var () function

|
Syntax: -
filter_var (var, filtername, options) 
Parameters : This function takes three parameters and described below:
  • var : This field is required. Indicates a variable to filter.
  • filter_name : Used to specify the identifier or name of the filter to use. The default is FILTER_DEFAULT, which results in no filtering. This is an optional field.
  • options : Used to specify one or more flags / options to use. Check each filter for possible choices and flags. It is also an optional field.
  • Return Value : Returns filtered data on success or FALSE on error.Below are a few different applications filter_var() functions:
    • Disinfect the line:
      In the example below, we clear the line Example: -

      $str = "

      GeeksforGeeks! < / h1 > " ; $newstr = filter_var ( $str , FILTER_SANITIZE_STRING); echo $newstr ;  
      ?>
      Output: -
      GeeksforGeeks! 

    • Check for an integer:In the example below, the filter_var() function is used to check if the variable $int is an integer. If $int is an integer, the output of the code below will look like "Integer is valid". If $int is not an integer, the output is: "Integer is not valid":Example: - $int = 200; if (filter_var ( $int , FILTER_VALIDATE_INT) === 0 || ! filter_var ( $int , FILTER_VALIDATE_INT) === false) { echo ( "Integer is valid" ); } else 
      { echo ( " Integer is not valid " ); }  
      ?>
      Output: -
      Integer is valid 
    • Check the IP address:
      The following example uses the filter_var() function to check if the $ip variable is a valid IP address: Example: - $ip = "129.0.0.1" ; if (! filter_var ( $ip , FILTER_VALIDATE_IP) === false) { echo ( " $ip is a valid IP address " ); } else { echo ( "$ip is not a valid IP address" ); }  
      ?>
      Output: -
      129.0. 0.1 is a valid IP address 
    • Sanitize and validate an email address:
      In the following example, the filter_var() function first removes all invalid characters from the $variable email and then checks if it is a valid email address: Example: - $email = "[email protected]" ;  
      // Remove all invalid characters from email $email = filter_var ( $email , FILTER_SANITIZE_EMAIL);  
      // Verify email address if (! filter_var ( $email , FILTER_VALIDATE_EMAIL) === false) { echo ( "$email is a valid email address" ); } else { echo ( "$email is not a valid email address" ); }  
      ?>
      Output: -
      gfg @ example.com is a valid email address 
    • Clean and validate URL:
      In the following example, the filter_var() function first removes all invalid characters from the URL, and then checks if $url is a valid url: Example: - $url = " https://www.engineerforengineer.org " ;  
      // Remove all invalid characters from the 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" ); }  
      ?>
      Output: -
      https://www.engineerforengineer.org is a valid URL 
    Link :
    http://php.net/manual/en/function.filter-var.php

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method