What does this reg expression do? HELP!

Discussion in 'PHP' started by Cobnut, Sep 24, 2009.

  1. #1
    I'm trying to debug someone elses code and the following line is causing a problem:

    if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", "string")) {
    TRUE
    }
    Code (markup):
    The 'string' in the ereg above is either a two alpha or three alpha word with totally normal a-Z chars and I would expect the condition to be FALSE but for some reason it's testing true. This is in a file that hasn't been edited for 6 months and has worked until today/yesterday - is there any possibility something has changed on the server?

    I can't read the ereg - anyone who can at least tell me exactly what the test is would be very helpful!!

    Jon
     
    Cobnut, Sep 24, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    For me it tests as false; make sure you're using "print" or some form of output. However, I'm not sure what you're trying to accomplish with this ereg. Could you give us the purpose? It would be a lot easier to fix it for you as I do not see in what normal instances that it would return true (probably because I don't know context). Right now this is what I see. Other than the extra hyphen after the 9 in your 2nd alphanumeric condition (wit the {0,61} attached).

    You're looking from the beginning of the line (^) to the end of the line ($) for any alphanumeric character once, then any alphanumeric character again anywhere from 0 to 61 recurrences and then again. Or you are looking for alphanumeric character.

    Regards,
    Dennis M.
     
    Dennis M., Sep 24, 2009 IP
  3. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Denis,

    Thanks for the speedy reply. As I said, this isn't my code so don't blame me if it's weird! It's part of an email address checking process and this section is checking the elements after the '@'. The text of the address following the '@' symbol is exploded (by '.') into an array and each element is then tested against the ereg I posted. So, for example, it would be testing 'domain', 'co' & 'uk' in sequence.

    As far as I can see, all it needs to do is check that they're acceptable chars for a domain.

    Is this what it's doing?

    Jon

    Sorry, misleading info, yes it SHOULD test as false for 'valid' entries such as 'domain', 'co' & 'uk'. If the condition is TRUE it sets a flag of 'Invalid Email Address'. Sounds weird but it's the way the testing has been built (assuming OK so looking for errors). Personally, I prefer to assume invalid and test for OK, but that's just me.
     
    Cobnut, Sep 24, 2009 IP
  4. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Hmm. There are many expressions you can use to test for email address. If you can't find one on google I can write one, but you also could use the split() (split the @ character) function and then just try to dnsresolve the domain name. If it resolves, it's acceptable, if not the host fails.

    Regards,
    Dennis M.
     
    Dennis M., Sep 24, 2009 IP