Help with Preg Match

Discussion in 'PHP' started by WhitneyM, Dec 17, 2009.

  1. #1
    So I can't figure preg_match out. If I am trying to verify that $variable is only letters and numbers and not blank allowing a period, -, space, @ symbol as well would something like this work?

    If(!preg_match("-@.a-z 1-9", $variable))
    {
    die("statement");
    }

    :confused:
     
    WhitneyM, Dec 17, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need a delimiting character at either end of your pattern. for example
    if(preg_match('%^[a-z0-9@. ]+$%i', $subject)) {
        die('Matched');
    }else{
        die('No Match');
    }
    PHP:
    Here's an explaination of what it does (from regexbuddy)
    ^[a-z0-9@. ]+$
    Assert position at the beginning of the string «^»
    Match a single character present in the list below «[a-z0-9@. ]+»
       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
       A character in the range between “a” and “z” «a-z»
       A character in the range between “0” and “9” «0-9»
       One of the characters “@. ” «@. »
    Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
    Code (markup):
    add more items to the [ ] if you want more characters matched
     
    JAY6390, Dec 17, 2009 IP
  3. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you! That helped a lot.
     
    WhitneyM, Dec 18, 2009 IP