Solved

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

Posted on 2008-06-11
9
252 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

11 Experts available now in Live!

Get 1:1 Help Now