I have a string that looks like this:
[2005]
one
two
three
[2004]
six
What would be the smoothest was to get an array from it that would look like this:
array(
['2005'] => "one \n two \n three",
['2005'] => "six",
)
… or maybe even get the inner array sliced into lines array…
I tried doing it with preg_split, which worked but didn’t give associative array keys so I didn’t have the year numbers as keys.
Is there any cool way of doing this without iterating through all the lines ?
/(\[[0-9]{4}\])([^\[]*)/will give you the date and whatever is after until the next one.Use the groups to create your array: With
preg_match_all()you get a $matches array where $matches[1] is the date and $matches[2] is the data following it.