sunjournal Posted June 22, 2006 Share Posted June 22, 2006 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, 24June 23June 23-25June 23June 23, 24, 25June 16-July 4I 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 22, 2006 Share Posted June 22, 2006 This works for all but the last (month ranges). You could incorporate them with the date and calendar functions.[code]<?php $data = <<<DATAJune 23, 24June 23June 23-25June 23June 23, 24, 25June 16-July 4DATA; ### 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.