Page 1 of 1

Split string problem

Posted: Fri Aug 07, 2009 3:47 am
by etherkye
I am trying to get my website capable of importing data from anouther program. Hence the data itself can not be changed.

Most lines of the file import are of the format

Code: Select all

"10000","Mr Anybody","AnyBusiness","ARoad","ATown","ACounty","AN1 1AN","Mr Any body","01708 766223","01708 737329","","","",0,"",0.00,0.00,0.00,500.00,"14",0,0.00,"4000","T1"
I can remove the " and split by , and then sort it into my database happily. However some people save the names with a , in them e.g.

Code: Select all

"10000","Mr Anybody","AnyBusiness","ARoad","ATown","ACounty","AN1 1AN",[color=#BF0000]"Mr body, Any"[/color],"01708 766223","01708 737329","","","",0,"",0.00,0.00,0.00,500.00,"14",0,0.00,"4000","T1"
Now when i split the string i have an extra element which means the database wouldn;t store correctly (in fact it hits a failsafe check and doesn't get tried.

What i need is a method to split the string into an array correctly. I cannot split by "," due to the numbers not containing "".

What would be the best way to split the above strings into the same array of 23 length?

Re: Split string problem

Posted: Fri Aug 07, 2009 4:03 am
by marty pain
There may well be an easier way than this, but it's early and I don't know any PHP functions that will do the job for you. So here it goes...

Swap " (speech marks) to ! (shout) (or any character you wish that is unlikely to be used) then explode the string into an array by , (comma)

Loop through the array and test to see if the value is numeric. If it is then move on to the next, if it's not make sure the string starts and ends with ! (shout). If it does then remove the ! (shout) and move on, if not concatenate the current test value with the next in the array, remove ! (shout), delete the element that was concatenated on to the end and move on to the next.

If it's just the names that have the extra , (comma) then you could just test that element of the array, ignoring the rest, and then remove the ! (shout) when finished.

Bit long winded but I hope it helps.

Re: Split string problem

Posted: Fri Aug 07, 2009 4:08 am
by etherkye
Takes long enouth to process the files normaly. I can't afford the time that it would take to do that. It's also highly inificent on 1000 lines of data. And annoyingly theres plenty of places they can place the ,

Re: Split string problem

Posted: Fri Aug 07, 2009 4:12 am
by marty pain
Good luck with it then.

Re: Split string problem

Posted: Fri Aug 07, 2009 4:39 am
by Mark Baker
Can't understand why you don't simply use fgetcsv to read in the file.... after all, it is just a standard CSV file

Re: Split string problem

Posted: Fri Aug 07, 2009 4:50 am
by marty pain
^^ Man, I just found that but you beat me too it. str_getcsv is the one I started looking at. That's one for the note book.

Re: Split string problem

Posted: Fri Aug 07, 2009 4:54 am
by etherkye
Thanks. Nice to see theres simple solutions XD