$date = '2010-07-09 16:45:37';
$reg = '/^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/';
preg_match($reg, $date, $matches);
print_r($matches);Code : Tout sélectionner
Array
(
[0] => 2010-07-09 16:45:37
[1] => 2010
[2] => 07
[3] => 09
[4] => 16
[5] => 45
[6] => 37
)
Mais, grâce à l'option d'assertion P, on peux nommer un groupe en faisant (?P<lenomdegroup>pattern)
$date = '2010-07-09 16:45:37';
$reg = '/^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})\s(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$/';
preg_match($reg, $date, $matches);
print_r($matches);Code : Tout sélectionner
Array
(
[0] => 2010-07-09 16:45:37
[year] => 2010
[1] => 2010
[month] => 07
[2] => 07
[day] => 09
[3] => 09
[hour] => 16
[4] => 16
[minute] => 45
[5] => 45
[second] => 37
[6] => 37
)