My string looks like so
February 2009
bla bla
March 2009
doo daa bla lbla
Septemer 2009
So I wrote this regex to split it up into months (which is what I want to do first, I think)
$regex = '/(.*)\s(\d){4}/i';
This matches them perfectly, except it throws away the actual string they were split on .. i.e. I want that information (as in February 2009, March 2009 etc)
I’ve tried mucking around with the preg_split() flags, but could not get what I wanted.
Should I be using a different approach? Is there an easy to split text via a regex but keep the text that was actually there?
Come to think of it, I could probably use `preg_match_all()’ here… I hope I just didn’t answer my own question in the answer – I’m going to post anyway to see what the community thinks.
Thanks
Put the splitting string into its own capture group. So given your example,
with a few modifications becomes:
If your matches array is called "$matches", $matches[0] will contain the whole match, $matches[1] the month, $matches[2] the splitting string, and $matches[3] the year.