-2

I try to extract dd-mmm-yyyy in PHP, but it's return blank array.

<?php

$reg = "/^[01][0-9]-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}$/";
$test = 'Composed by super, user123 to super,user123 on 31-Mar-2020 11:29 with';


preg_match( $reg, $test, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);


?>  

any idea ?

questionasker
  • 2,536
  • 12
  • 55
  • 119

1 Answers1

0

In your regex there are some issues:

  • replace ^ and $ with \b;
  • you are considering only dates with days starting with 0 and 1;
  • days should be (?:0?[1-9]|[12]\d|3[01]);
  • years should be (?:20|19)\d{2};
  • remove PREG_OFFSET_CAPTURE from preg_match.
<?php
$reg = "/\b(?:0?[1-9]|[12]\d|3[01])-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(?:20|19)\d{2}\b/";
$test = 'Composed by super, user123 to super,user123 on 31-Mar-2020 11:29 with';

preg_match($reg, $test, $matches);
print_r($matches);
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34