Search the Community
Showing results for tags 'phpexplode'.
-
Hi guys, Users of my website will be copying and pasting their work schedule from another website that displays it in a html table, as shown below, in to a textarea on my website which I can then decode and store in a mysql db. To do this I am trying to put every value in to an array with the associated column name/key. |Date| |Duty| |Dep| |Start| |End| |Arr| 4 Apr 14, Fri 3005 (Z) CIA 04:15 Z 07:05 Z STN 4 Apr 14, Fri 3002 STN 07:45 Z 10:15 Z CIA 5 Apr 14, Sat OFF (Z) CIA 00:00 Z 21:00 Z The small problem being both the 'Duty' and 'Arr' column can have any numerical, text or blank spaces in them. All the other columns have the same format in every row. (such as the 'Dep' column is always 3 uppercase characters etc). I have tried to separate the parts of each line as you can see in the code below but this would not be consistent due to the extra spaces the sometimes occur in the 'Duty' column. Does anyone see a way to make this fault proof?! Thanks <?php $lines = explode("\n", $_POST['test']); $sector = 1; foreach($lines as $line) { if(substr($line, 0, 4)=='Date') { //Skip header line continue; } $parts = preg_split('/\s+/', $line); echo 'Sector '.$sector.'<br />'; $date = $parts[0] . '/' . $parts[1] . '/20' . trim($parts[2], ','); $duty = $parts[4]; $dep = $parts[5]; $begin = $parts[6]; $end = $parts[8]; $arr = $parts[10]; $sector++; } ?>