-1

I am using this next pattern for HTML (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d.

I want exactly the same pattern for PHP preg_match() for server-side validation. Does someone know this? Is there some kind of converter or something.

The pattern is for 12/12/1999 and also 12-12-1999 and as you can see it only allows year 1900 and 2000.

I searched online for the pattern, but I couldn't find the combination pattern on having 12/12/1999 and also 12-12-1999 + 1900 and only the year 2000.

And does someone know a good website to convert these kind of things or to make preg_match PHP or patterns HTML?

halfer
  • 19,824
  • 17
  • 99
  • 186
Gester
  • 113
  • 7
  • 1
    Did it not work with preg_match? Did you try `~` as a delimiter instead of `/`? – The fourth bird Jun 25 '20 at 18:59
  • its a date, just include a `if(DateTime::createFromFormat('d/m/Y', ..` as part of your validation, its not expensive calling it twice and can support any formats – Lawrence Cherone Jun 25 '20 at 19:21
  • You need `preg_match('~^(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\1(?:19|20)\d{2}$~', $text)`, else you will match the pattern in longer strings and you might match `12-12.1988` like strings where the delimiters are not consistent. – Wiktor Stribiżew Jun 25 '20 at 19:54
  • @WiktorStribiżew Thanks for your help Wiktor but it worked on next solution. `preg_match('/(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d/', '12-12-9999', $matches);` – Gester Jun 25 '20 at 19:56
  • So, you want to match `12-12-9999` in `323434512-12-99993423534`? – Wiktor Stribiżew Jun 25 '20 at 19:59
  • @WiktorStribiżew Ooh shit, oke. Yeah i noticed it just now. So yours will have a limit on 2-2 and 4 ? – Gester Jun 25 '20 at 21:36
  • @WiktorStribiżew Does it also check if its a valid email like this is not > 33-33-2020? Or not – Gester Jun 25 '20 at 21:40
  • `33-33-2020` is not a valid email. My suggestion is based on the fact HTML patterns are anchored by default while `preg_*` patterns are not. I also think the delimiters should be consistent, else, you may match something that is not what you need. – Wiktor Stribiżew Jun 25 '20 at 21:42
  • @WiktorStribiżew Sorry wrong question from me. My question is if your code also checks if the next date is wrong `33-33-2020` Or does it continue with your code. Because you said earlier `1212-1212-9999` this will be blocked right? – Gester Jun 25 '20 at 21:46
  • See [this updated demo](https://regex101.com/r/tVEwwD/4). Your regex does not match `33-33-2020`. – Wiktor Stribiżew Jun 25 '20 at 22:03
  • @WiktorStribiżew It's perfect, very handy website also. If you make it inside a answer. I will accept yours and unaccept the one below? Because people may be searching for this i want the best solution to be on top. – Gester Jun 25 '20 at 22:19
  • It still occurs to me https://stackoverflow.com/questions/11974490/converting-javascript-regex-to-php gives you all you need to convert any JS regex to PHP. – Wiktor Stribiżew Jun 25 '20 at 22:29
  • @WiktorStribiżew Yeah still little unclear how to do it. Too bad there is not website to convert them. I am checking the Javascript Regex and the website you gave me shows no error if i use them AS php regex. Need to test it still. – Gester Jun 25 '20 at 23:24
  • @WiktorStribiżew Hey Wiktor do you maybe this question i made day's ago nobody answers. It's about recaptcha V3 > https://stackoverflow.com/questions/62542914/can-someone-confirm-please-recaptcha-v3-returns-score-0-1-after-plus-minus-20-t – Gester Jun 26 '20 at 01:11

1 Answers1

0

All you need to do is escape the forward slashes (/). Escape forward slashes with a backslash, like \/

So this would be your pattern:

(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d

preg_match('/(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d/', '12-12-9999', $matches);

Here's an example on regex101 of it matching both dates using the PHP regex engine (PCRE):

https://regex101.com/r/tVEwwD/3

OR, another alternative is to just use a different delimiter, like so:

preg_match('~(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d~', '12/12/9999', $matches);

Notice the ~ character, before and after the regex pattern. That's the delimeter.

Bryan Elliott
  • 4,055
  • 2
  • 21
  • 22
  • Oh yeah i think someone downvoted because there is no limit on the dates as Wiktor mentioned. `preg_match('~^(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\1(?:19|20)\d{2}$~', $text)` I still need to test it though – Gester Jun 25 '20 at 21:37