Preg_match() takes extra strings from the target string

Asked 1 years ago, Updated 1 years ago, 66 views

Code

<?php
$test = 'this is October 02, 2017 today';
$months="/(January | February | March | April | May | June | July | August |
   September | October | November | December) [0-3] [0-9], 20 [0-9] +/";

preg_match($months,$test,$date);

var_dump($date);
?>

I want to get October02,2017.
Output Results

array(2){[0]=>string(16) "Octover02, 2017"[1]=>string(7) "Octover"}

The target string has been removed, but the October itself has also been removed.
Please tell me why this is happening.

php regular-expression

2022-09-30 17:02

1 Answers

Subpattern has a variety and simple () captures.The return of October is also the result of the capture.Use (?:) for subpatterns that you do not want to capture.

$months="/(?: January | February | March | April | May | June | August | September | October | October | November | December) [0-3][0-9], 20[0-9]+/";


2022-09-30 17:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.