1

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.

1
  • Can you show us what your preg_split() code looks like? Commented Jan 29, 2012 at 17:11

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Go with jburbage's answer, his solution is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.