unistake Posted March 11, 2014 Share Posted March 11, 2014 Hi guys, I am trying to find a way for a website user to copy a standard html table from a website and paste it in to a form on my website, which would then upload it to php to a mysql db. The table they would copy follows the same format however the fields could sometimes have either or numberical or characters as values. An example of a table I would be trying to upload to my DB is this: "Date Duty Dep Begin End Arr 1 Apr 14, Wed 7743 HME 04:40 Z 06:20 Z AWY 1 Apr 14, Wed 6473 AWY 06:45 Z 08:55 Z FRG 1 Apr 14, Wed 1231 FRG 09:20 Z 11:25 Z AWY 1 Apr 14, Wed 2222 AWY 11:50 Z 13:25 Z HME 2 Apr 14, Tue STDY0300-Z HME 03:00 Z 15:00 Z 3 Apr 14, Thu 6666 HME 05:00 Z 06:05 Z WAG 3 Apr 14, Thu 5555 WAG 06:30 Z 07:40 Z HME 3 Apr 14, Thu 2222 HME 08:05 Z 10:50 Z NWR 3 Apr 14, Thu 1111 NWR 11:15 Z 13:50 Z HME 4 Apr 14, Fri 4444 HME 04:15 Z 07:05 Z CKC 4 Apr 14, Fri 2222 CKC 07:45 Z 10:15 Z HME" Does anyone know the best way to try and separate the columns in each row to process with PHP? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/286883-copy-and-paste-a-html-table-to-php/ Share on other sites More sharing options...
Solution Psycho Posted March 11, 2014 Solution Share Posted March 11, 2014 Well, let's step back a second. By copying and pasting the table you are talking about the output of the table and not the source code, right? Since the output is being copied onto the user's clipboard, I can't guarantee that the "copied" output would always be exactly the same based upon browser or, especially, operating system. A MAC may copy the content much differently than a PC. However, based upon the example you have posted, the output was simply adding a space between each cell. So, the problem here is you need to differentiate between spaces that exist within the data field and the spaces that delineate the different pieces of data. If the data will always be in the same format then you can do it be creating the rules on processing the data. Based on what I see, every line will have the same number of spaces (i.e. there are no fields where some values have more or less spaces than other values in the same column). So, the easiest solution, IMO, is to simply explode the lines and then use the parts you want. Here is some sample code with absolutely no verification that this will work for all possibilities $lines = explode("\n", $_POST['input']); foreach($lines as $line) { if(substr($line, 0, 4)=='Date') { //Skip header line continue; } $parts = explode(' ', $line); $date = $parts[1] . ' ' . $parts[0] . ', 20' . trim($parts[2], ','); $duty = $parts[4]; $dep = $parts[5]; $begin = $parts[6]; $end = $parts[8]; $arr = $parts[10]; echo "Date: {$date}<br>\n"; echo "Duty: {$duty}<br>\n"; echo "Dep: {$dep}<br>\n"; echo "Begin: {$begin}<br>\n"; echo "End: {$end}<br>\n"; echo "Arr: {$arr}<br><br>\n"; } Part of the sample output Date: Apr 1, 2014 Duty: 7743 Dep: HME Begin: 04:40 End: 06:20 Arr: AWY Date: Apr 1, 2014 Duty: 6473 Dep: AWY Begin: 06:45 End: 08:55 Arr: FRG Date: Apr 1, 2014 Duty: 1231 Dep: FRG Begin: 09:20 End: 11:25 Arr: AWY . . . Quote Link to comment https://forums.phpfreaks.com/topic/286883-copy-and-paste-a-html-table-to-php/#findComment-1472174 Share on other sites More sharing options...
unistake Posted March 12, 2014 Author Share Posted March 12, 2014 Thanks Psycho, your a legend! Quote Link to comment https://forums.phpfreaks.com/topic/286883-copy-and-paste-a-html-table-to-php/#findComment-1472339 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.