Question

Using below pattern matching using preg_match in PHP

if(preg_match('/^[0-9]{2}/[0-9]{2}/[0-9]{4}$/', $dateVar ) { Do Something }

This matches 04/22/2002 but not 4/22/2002 As above pattern is only matching 2 characters for MM and DD from 0-9.

Can any one please suggest to how to match 1-2 characters using preg_match for MM and DD pattern.

Était-ce utile?

La solution

You can try below one

$dateVar = '4/22/2002'; //4/22/2002

if(preg_match('/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/', $dateVar, $match )){
    print_r($match);
}

output-
Array
(
    [0] => 4/22/2002
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top