Jump to content


Photo

Parsing text file


  • Please log in to reply
1 reply to this topic

#1 sunjournal

sunjournal
  • New Members
  • Pip
  • Newbie
  • 1 posts
  • LocationLewiston, ME

Posted 22 June 2006 - 08:20 PM

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.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 22 June 2006 - 09:27 PM

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

<?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);
            }

        }
    }

?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users