Jump to content

exploding a string at the correct point


unistake

Recommended Posts

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++;
}
?>
Edited by unistake
Link to comment
Share on other sites

Why didn't you post this in response to the solution I provided in your other thread? It's poor form to take someones code like that and then simply turn around and ask for help with it.

 

The solution I provided worked perfectly with the (sparse) information you provided. I even included the explicit disclaimer in that post

 

 

If the data will always be in the same format then you can do it be creating the rules on processing the data.

 

The data you provided was in the same format and there was nothing to show otherwise. So, now we know differently. As I also stated in that thread you are relying on several things that are completely out of your control. Namely, "what" is getting copied/pasted into the input field. Based upon the browser and OS of the user it could be very different for one user to another. So, no matter what solution you come up with, it will likely have flaws (if not now, later).

 

So, having said all that, you can probably get very close to perfection. You state only the Duty and Arr fields will have variable content format, but all the other fields will adhere to a specific format. If that is the case, then I would go another route entirely: RegEx. Which I considered previously but, based on the information you provided, it didn't seem it was needed. I assume that Duty will always have a value though. I am also assuming that Dep will be exactly a three letter value.

$output = '';
$lines = explode("\n", $_POST['input']);
foreach($lines as $line)
{
    if (preg_match("#([^,]+), \w{3}[\s]+(.+?)[\s]+(\w{3})[\s]+(\d\d:\d\d) Z[\s]+(\d\d:\d\d) Z[\s]+(.*)#", $line, $match))
    {
        $date = date('m/d/Y', strtotime($match[1]));
        $duty = $match[2];
        $dep = $match[3];
        $begin = $match[4];
        $end = $match[5];
        $arr = $match[6];
        $output .= "<tr><td>{$date}</td><td>{$duty}</td><td>{$dep}</td><td>{$begin}</td><td>{$end}</td><td>{$arr}</td></tr>\n";
    }
}

echo "<table border='1'>\n";
echo "<tr><th>Date</th><th>Duty</th><th>Dep</th><th>Begin</th><th>End</th><th>Arr</th></tr>\n";
echo $output;
echo "</table>";

This includes some additional logic to hopefully capture any differences in copy/paste values based on system. Namely when copy/pasting the table data the values between columns can be separated by one (or more) white-space characters: space, tab, etc.

Edited by Psycho
Link to comment
Share on other sites

Hi Psycho,

 

Apologies I wrote a reply to my previous thread but somehow got logged out and decided to just create another now that I have the full idea of the information that would be provided.

 

Thank you so much for your time, the code above would work perfectly.

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.