SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 problems, check variable for special charecters and valid email check

    I was wondering how can i check if a php variable has these special characters

    Code:
    *|,\":<>_-!^+[]{}?.`~\';()@&$#&#37;
    i heard it can be done with preg_match(), but only 1 charecter at a time, but i want to check if the string has any of above mentioned charecters.

    Also how can i check if a php variable has a valid email structure?. I done all this well in javascript, but I'm doing php checking so that everyone can access the site without any problems.

    Thanks in advance.

  2. #2
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suck at patterns but I have this for checking if a email is valid:
    PHP Code:
    <?
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$"$email)) {
    //error
    }
    ?>
    Credits probably goto someone at php.net, not sure where I got it from.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    Ah still one problem left, the special character check, anyone give it a try?

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    White list approach is better. Simply check if the variable contains only valid characters:

    PHP Code:
    $ok preg_match("~^[valid chars]+$~D"$var); 
    "valid chars" can be something like "\w" (letters and digits) or "A-Za-z " (only english letters and a space) etc.

    See pattern syntax for more info.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have to specify which characters it shouldn't allow, cause of a password string. for userid it can only be letters and numbers, but for password it can be letters, numbers and underscores.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $ok preg_match("~^[A-Za-z0-9_]+$~D"$var); 
    checks if the string contains only letters, numbers and underscores.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    PHP Code:
    $ok preg_match("~^[A-Za-z0-9_]+$~D"$var); 
    checks if the string contains only letters, numbers and underscores.
    doing the following returns #ok = 1, right? meaning it contains only letters, numbers and underscores.
    PHP Code:
    $ok preg_match("~^[A-Za-z0-9_]+$~D"$var); 
    doing the following checks for only letters and numbers?
    PHP Code:
    $ok preg_match("~^[A-Za-z0-9]+$~D"$var); 
    just want to be perfectly clear

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes and yes

  9. #9
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a doubt.

    supppose.

    PHP Code:
    $userid "apple2^6"
    $check preg_match("~^[A-Za-z0-9]+$~D"$userid); 
    $check will return 1, because there are letters/numbers?. Hope it doesn't work like that! or will it return 0 because it contains ^ charecter?.


    Thanks for all your help

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^ means "beginning of the string"
    $ means "end of string"
    + means "one or more"
    [] means "one of"

    ^[A-Za-z0-9]+$ = start - one or more letters or numbers - end.

    In others words, the above checks if a string contains ONLY letters and numbers, and nothing more. To check if it contains AT LEAST ONE letter or number, among with possibly other characters, remove ^ and $ from the expression.

    Please do read the manual page I linked to above.

  11. #11
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw that manual only complicates things, no offense to you of course!

    ah so it checks if the string only contains letters/numbers, meaning it would return a 0 if had special characters?

    Anyway thanks for all your help, greatly appreciate it

  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, php manual is the technical description of how things work, its purpose is rather not pedagogical. If you're looking for easy to read tutorials on the topic, http://www.regular-expressions.info seems to be the right place.

  13. #13
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can gank all the regular expressions you want from this site. I grabbed a couple of nice ones there today.

    http://regexlib.com/Default.aspx

  14. #14
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With php 5.2 up you can use the filter functions to properly filter email addresses.
    ref.filter.php


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •