Regular expression to enforce an empty field

Discussion in 'PHP' started by sarahk, Sep 5, 2008.

  1. #1
    I'm programming a honeypot for vBulletin and I have the opportunity to have the field tested against a regular expression.

    Does anyone know how I'd do this to check that the field is empty?

    thanks
     
    sarahk, Sep 5, 2008 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't see why you'd want to use regex for this. Just check if it evaluates to boolean false ("if (!$field)...")
     
    Shoro, Sep 5, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    ^ I'm guessing this has something to do with vB's options? Anyway, using regular expression, you could do this:
    
    '~^$~'
    
    PHP:
     
    nico_swd, Sep 6, 2008 IP
    sarahk likes this.
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, you can use the above regex, although why you can't just test the length of the field with strlen() I'm not too sure:confused:
     
    JAY6390, Sep 6, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I've seen you using strlen() a lot in your blog, and I think it's kinda redundant too. You can just do:
    
    if (!$field)
    {
        // ...
    }
    
    PHP:
    ... which returns false on empty strings too, and it's a function call less. :)

    Or even:
    
    if (!$field = trim($field))
    {
        // ...
    }
    
    PHP:
    ... to make sure the field doesn't contain just empty spaces.

    EDIT:

    And least, you could do:
    
    if (!empty($field))
    {
        // ...
    }
    
    PHP:
    ... which does the same as isset() and (bool)$field at the same time. It's faster too, since it's a language construct.
     
    nico_swd, Sep 6, 2008 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah I guess. Thanks
     
    JAY6390, Sep 6, 2008 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,906
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #7
    Testing the field contents is an option but requires writing a plugin which is one more thing to migrate, test etc.

    If I can use the existing architecture to test the field (which is why the regex test is there in the first place) I am reducing the risk of problems in the future.
    I'll give that a go, thanks :)
     
    sarahk, Sep 6, 2008 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ah I see :)
     
    JAY6390, Sep 6, 2008 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,906
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #9
    ^$
    PHP:
    did the trick, thanks
     
    sarahk, Sep 8, 2008 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    I guess vB adds the delimiters automatically then. :)
     
    nico_swd, Sep 8, 2008 IP