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
https://forums.phpfreaks.com/topic/12671-parsing-text-file/
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
https://forums.phpfreaks.com/topic/12671-parsing-text-file/#findComment-48616
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.