Solved

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

Posted on 2008-06-11
9
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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
 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

628 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