Tricky php string matching

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 ?

Answers


/(\[[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.


Need Your Help

Is there any good way to make a non-contiguous array as contiguous 1D array?

excel-vba vbscript

Just out curiosity -Is there any good way to make a non-contiguous array as contiguous 1D array?

C# Value Type Class Template

c# struct

I'm writing/written a value type struct and was wondering if there was a guide that contains a standard template for method signatures and interface implementation. I thought I'd start by looking ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.