phppreg-matchereg

Need to switch from ereg() to preg_match()


I need to know what this line of code does, tried to figure it out because i have to build it with preg_match() but I didn't understand it completely:

ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})", $date)

I know it checks a date, but i don't know in which way.

thanks for some help


Solution

  • Let's break this down:

    ([0-9]{1,2})
    

    This looks for numbers zero through nine (- indicates a range when used in brackets []) and there can be 1 or two of them.

    .
    

    This looks for any single character

    ([0-9]{1,2})
    

    This looks for numbers zero through nine and there can be 1 or two of them (again)

    .
    

    This looks for any single character (again)

    ([0-9]{4})
    

    This looks for numbers zero through nine and there must be four of them in a row

    So it is looking for a date in any of the following formats:

    More will fit that pattern so it isn't a very good regex for what it is supposed to validate against. There are lots of sample regex patterns for matting dates in this format so if you google it you'll have a PCRE in no time.