Jump to content

Parsing text file


sunjournal

Recommended Posts

Hello,
I'm trying to pluck out some dates from flat text file. I can open it and loop through each line etc... But man, I have no clue how to deal with getting these dates out.

I have been able to isolate just the dates, but I have some consecutive dates I don't know how to deal with, for example:

June 23, 24
June 23
June 23-25
June 23
June 23, 24, 25
June 16-July 4

I now need to loop through and pick out each date and insert it into a table. The weird thing is, the users have typed in multiple dates in different ways!

I can probably write a boat load of "if" statements to deal with this, but I thought I would throw this out there and see if anybody else had a better idea.

Thanks in advance for at least reading this post. Regards.
Link to comment
Share on other sites

This works for all but the last (month ranges). You could incorporate them with the date and calendar functions.

[code]<?php
    
    $data = <<<DATA
June 23, 24
June 23
June 23-25
June 23
June 23, 24, 25
June 16-July 4
DATA;

    ### Array dates.
    $dates = split("\n", $data);
    ### Work with each date.
    foreach ($dates as $date) {
        echo "<hr /><b>$date</b>";
        ### Split date into pieces by comma, hyphen, or space.
        $date_pieces = preg_split('/[, ]/', $date, -1, PREG_SPLIT_NO_EMPTY);
        ### Show split.
        echo '<pre>', print_r($date_pieces, true), '</pre>';
        ### Set up and work with pieces.
        $month = '';
        $day = '';
        foreach ($date_pieces as $piece) {
            $piece = trim($piece);
            ### Look for a month.
            if(preg_match('/^([A-z]+)$/', $piece, $matches)) {
                $month = $matches[1];
            }
            ### Look for a day.
            if(preg_match('/^(\d+)$/', $piece, $matches)) {
                $day = $matches[1];
            }
            ### Look for days.
            if(preg_match('/^(\d+)-(\d+)$/', $piece, $matches)) {
                $days_start = $matches[1];
                $days_end = $matches[2];
                if ($days_end <= $days_start) {
                    echo 'Invalid date range.';
                    exit;
                }
                $day = range($days_start, $days_end);
            }    
            ### Output if we have a month and a day.
            if ($month && $day) {
                if (!is_array($day)) {
                    $day = array($day);
                }
                foreach ($day as $item) {
                    echo "<b>Pair:</b> $month $item<br />";
                }
                unset($day);
            }

        }
    }

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.