Simple Regular expression question

Discussion in 'PHP' started by slaydragon, Jun 11, 2008.

  1. #1
    Code example.

    #^[a-z0-9-]+\.[a-z0-9-\.]{2,8}$#si
    PHP:
    may i know what does the {2,8} $ means and what does the si means?

    Help appreciated alot if anyone can explain the expression in layman terms.. thanks in advance:)
     
    slaydragon, Jun 11, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    rohan_shenoy, Jun 11, 2008 IP
    slaydragon likes this.
  3. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    si are modifiers for the pattern

    s means a . (dot) will match new lines
    i means the pattern is case-insensitive
     
    lui2603, Jun 11, 2008 IP
    slaydragon likes this.
  4. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    can someone help me see if my reg expression is correct..

    ^([a-zA-z\-]{3,*})+$
    PHP:
    i want to allow only letters and dash(-). and minimum 3 characters.. this is what i come out with.. is it correct?
     
    slaydragon, Jun 11, 2008 IP
  5. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi, i found out some problem with the code i use above. The letters and dash are correct, but the min 3 characters seem wrong, i couldn't figure that out. any advice or light appreciated.
     
    slaydragon, Jun 12, 2008 IP
  6. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #6

    I have not found solution with regexp to limit the length, BUT, you want the input to be letters and dash AND minimum length is 3, so i have other solution :D

    
    $input = 'something';
    if (preg_match('/^([a-z\-]+)+$/i', $input) && strlen($input) >=3) {
       echo('good');
    }
    
    PHP:
     
    xrvel, Jun 14, 2008 IP
    slaydragon likes this.
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    N or more characters is written as
    {N,}
    Code (markup):
    Not as
    {N,*}
    Code (markup):
     
    joebert, Jun 14, 2008 IP
  8. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hi, what is the regular expression to allow whitespace is it

    ^([a-zA-z\-\s]{3,})+$ 
    Code (markup):
    ??? is whitespace \s?
     
    slaydragon, Jun 14, 2008 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes, \s is any whitespace character.
     
    zerxer, Jun 14, 2008 IP