Solved

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

Posted on 2008-06-11
9
256 Views
Last Modified: 2013-12-13
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");

$lines=explode("

", $openfile);
 

	

$count=count($lines);

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
 

$email=$thisline[0];

$address=$thisline[1];

$name=thisline[2];
 

// 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

0
Comment
Question by:Devain
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Accepted Solution

by:
nplib earned 167 total points
ID: 21761916
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.
0
 
LVL 4

Assisted Solution

by:albuitra
albuitra earned 167 total points
ID: 21761974
have you enclosed the parts of the line ?
if for example you have something like this:
"email","addres,secondaddres", "name"
or
email TAB,TAB addres,secondaddres TAB,TAB name
you can try with replace the (",") with something like (";"), then explode by ";"

0
 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 166 total points
ID: 21762002
First you could create a function that runs through each member of  the array doing a string replace:
http://us2.php.net/str_replace
0
 

Author Comment

by:Devain
ID: 21762217
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Devain
ID: 21762238
Again Thank you
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 21762256
So we did help you, didn't we?
0
 
LVL 4

Expert Comment

by:albuitra
ID: 21762410
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 ?
0
 

Author Comment

by:Devain
ID: 21770400
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
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now