Split string problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
etherkye
Forum Newbie
Posts: 21
Joined: Fri Aug 07, 2009 3:40 am

Split string problem

Post 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?
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Split string problem

Post 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.
etherkye
Forum Newbie
Posts: 21
Joined: Fri Aug 07, 2009 3:40 am

Re: Split string problem

Post 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 ,
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Split string problem

Post by marty pain »

Good luck with it then.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Split string problem

Post 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
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Split string problem

Post 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.
etherkye
Forum Newbie
Posts: 21
Joined: Fri Aug 07, 2009 3:40 am

Re: Split string problem

Post by etherkye »

Thanks. Nice to see theres simple solutions XD
Post Reply