I'm trying to replace variables like {{{month}}} in a template to the current month and {{{month+1}}} to current month + 1. That's not the hardest part of my code, except that the regex I wrote doesn't yield expected results.
$string = '{{{year}}}{{{month+1}}}';
preg_match_all('/{{{(?:([yY])ear|([mM])onth|([dD])ay)(?:(?<operation>[-|+])(?<amount>[1-9]+))?}}}/m', $string, $matches);
var_dump($matches);
Why do I have so much empty array entries? I was expecting
[0] => array('{{{year}}}', '{{{month+1}}}')
[1] => array('y', 'm')
[2] => array('', '+')
[3] => array('', '1')
What am I doing wrong?
The respond of the above code is:
array(8) {
[0]=>
array(2) {
[0]=>
string(10) "{{{year}}}"
[1]=>
string(13) "{{{month+1}}}"
}
[1]=>
array(2) {
[0]=>
string(1) "y"
[1]=>
string(0) ""
}
[2]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "m"
}
[3]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(0) ""
}
["operation"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
[4]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
["amount"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
[5]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
}