• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

How do I fix problems with exploding and array that has comma's in address

Hello Experts

I am having problems with exploding an array that has commas in the array is there a way to fix this?

Heres my example

$openfile= file_get_contents("$uploadfile");
", $openfile);
for ($k = 1; $k <= $count; $k++) {
$thisline=explode(",", $lines[$k]);
// What happens here is if the address has a comma in it then it assumes that its a new row
// This puts this into the database
// email is gogo@gogo.com
//address is 1111 gogo ln,FL
// name = gogo
//email = email
//address = parts of the address before the comma (eg 111 gogo ln)
//name   =  puts the rest of the address and name is left out  (eg FL)

Open in new window

  • 3
  • 2
  • 2
  • +1
3 Solutions
you need to use a less likely character combination as your string separator.

explode can only read the characters of the string, it can't interpret which commas you want to use as separators and which ones you don't want.
have you enclosed the parts of the line ?
if for example you have something like this:
"email","addres,secondaddres", "name"
email TAB,TAB addres,secondaddres TAB,TAB name
you can try with replace the (",") with something like (";"), then explode by ";"

First you could create a function that runs through each member of  the array doing a string replace:
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

DevainAuthor Commented:
Thank you your suggestions made me think of a eaiser way Appericate the help.  This was so simple and I should have thought of it eairler.

What I did

1.  Load the CSV file and replace all of the text that had comma's in it with a space then uploaded the file this fixed everything.

Thanks Again

DevainAuthor Commented:
Again Thank you
So we did help you, didn't we?
1.  is not a solution. If you replace ALL comma with spaces, now you have to explode by space, and that is bad.
2.With do you need to reupload the file ? you can read a line, replace the string in memory, explode the string, then insert.
3. If we help you, what about the points ?
DevainAuthor Commented:
Yes you did help I appoligize for the misunderstanding I read all posts accepted all of them not mind I quess the comments when accepting the solutions messed it up.

The solution was realized in this manner with all your suggestions.  

1.  The file was already TAB Delimates as it was a csv file.
2.  Rmoving the comma's in the Tab Delimated file before upload fixed the problem.
3. Since I was exploding twice once by tab delimation which create an array of comma delimated items there were extra commas which were dervived from the addresses this messed things up.

With your suggestions I realized this.

I am not accepting my own answer I am awarding points to all that posted a response
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now