• PHP FILTER_VAR – Your new best friend

    filter_var(variable, filter, options)

    variable : Required. Specifies the variable to filter
    filter : Optional. Specifies the ID of the filter to use. Default is FILTER_SANITIZE_STRING. Check the Complete PHP Filter Reference for possible filters
    options : Optional. Specifies an associative array of flags/options or a single flag/option. Check each filter for possible options and flags


    A filter ID can be an ID name (like FILTER_VALIDATE_EMAIL) or an ID number (like 274)

    If you have spent much time writing code, you’ve probably had to validate an email address. I’ve seen lots of methods, most of the best are based on regular expressions of one type or another (although I did see one based on multiple different strstr() calls, though I never tried it, so I can’t speak to its effectiveness). However, I have long since given up trying to maintain an effective regex that will effectively separate the wheat from the chaff. Instead, I have grown to love one of PHP’s most valuable functions: filter_var.

    filter_var() is a very useful function (actually one member of a family of functions that can filter – and validate – variables, arrays and inputs). Basically, you feed it a variable and a filter, and it gives you a result. Sounds pretty innocuous, but I promise that once you get to know filter_var(), you will love her.

    1
    
     

    The output of the code should be: E-mail is not valid

Comments on this post

Leave a Reply

  • Security Code :


    4 − one =