Solved

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

Posted on 2008-06-11
9
262 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
Independent Software Vendors: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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