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

    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best laptop for development

    $499+
    Gifts for programmers

    Best laptop for Cricut Maker

    $299+
    Gifts for programmers

    Best laptop for hacking

    $890
    Gifts for programmers

    Best laptop for Machine Learning

    $699+
    Gifts for programmers

    Raspberry Pi robot kit

    $150

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically