Solved

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

Posted on 2008-06-11
9
261 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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