Mike Thrussell
Mike Thrussell

Reputation: 4515

php split string into array by date (yyyy-mm-dd)

I have a plain text journal I'm trying to format in php. I'd like to split each entry into an array containing date and text.

Using preg_split I can do this, however it removes the date altogether!

The txt file looks something like this:

2012-01-28

text text text

2012-01-14

some other text

2011-12-07

a previous entry

etc.

Upvotes: 1

Views: 1975

Answers (2)

Jason Burbage
Jason Burbage

Reputation: 133

the PREG_SPLIT_DELIM_CAPTURE flag will prevent your dates from being removed when you split, as long as the date is parenthesized in your pattern string. e.g.:

$array = preg_split('/(\d{4}-\d{2}-\d{2})/', $text, null, PREG_SPLIT_DELIM_CAPTURE);

Now your dates are in the odd indices of the array and your content is in the even indices.

Upvotes: 2

Mikulas Dite
Mikulas Dite

Reputation: 7941

By all means you can preg_match_all:

$matches = array();
preg_match_all('~(?P<date>\d{4}-\d{2}-\d{2})\s*(?P<text>.*)~ms', $subject, $matches);

Might need to meddle with it a bit to fit your content.

Results into a structure like

matches => {
    date(3) => {"2012-01-28", ... }
    ...
    text(3) => {"text text text", ...}

which can easily be utilized or transformed into custom sctructure.

Upvotes: 2

Related Questions