Domanda

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

È stato utile?

Soluzione

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:

  • 04 18 1973
  • 04-18-1973
  • 04/18/1973
  • 04.18.1973

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.

Altri suggerimenti

It's a relatively simple regular expression (regex). If you're going to be working with regex, then I suggest taking a bit of time to learn the syntax. A good starting place to learn is http://regular-expressions.info.

"Regular expressions" or "regex" is a pattern matching language used for searching through strings. There are a number of dialects, which are mostly fairly similar but have some differences. PHP started out with the ereg() family of functions using one particular dialect and then switched to the preg_xx() functions to use a slightly different regex dialect.

There are some differences in syntax between the two, which it is helpful to learn, but they're fairly minor. And in fact the good news for you is that the pattern here is pretty much identical between the two.

Beyond the patterns themselves, the only other major difference you need to know about is that patterns in preg_match() must have a pair of delimiting characters at either end of the pattern string. The most commonly used characters for this are slashes (/).

So in this case, all you need to do is swap ereg for preg_match, and add the slashes to either end of the pattern:

$result = preg_match("/([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})/", $date);
                      ^                                    ^
                  slash here                           and here

It would still help to get an understanding of what the pattern is doing, but for a quick win, that's probably all you need to do in this case. Other cases may be more complex, but most will be as simple as that.

Go read the regular-expressions.info site I linked earlier though; it will help you.

One thing I would add, however, is that the pattern given here is actually quite poorly written. It is intending to match a date string, but will match a lot of things that it probably didn't intend to.

You could fix it up by finding a better regex expression for matching dates, but it is quite possible that the code could be written without needing regex at all -- PHP has some perfectly good date handling functionality built into it. You'd need to consider the code around it and understand what it's doing, but it's perfectly possible that the whole thing could be replaced with something like this:

$dateObject = DateTime::CreateFromFormat($date, 'd.M.Y');

It looks like it would be pretty much agnostic in its matching.

You could interpret it either as mm.dd.yyyy or dd.mm.yyyy. I would consider modifying it if you were in fact trying to match/verify a date as 00.00.0000 would be a match but is an invalid data, outside of possible historic context.

Edit: I forget '.' in this case would match any character without escaping.

this do the same, i have only replace [0-9] by \d, and the dot (that match all) by \D (a non digit, but can replace it by \. or [.- ])

preg_match("~\d{2}\D\d{2}\D\d{4}~", $date)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top