Solved

How can I remove the commas when importing a cvs file through PHP?

Posted on 2008-10-23
3
249 Views
Last Modified: 2013-11-19
I am importing a CVS file and The import works fine but I can't seem to get rid of the comma just before the email.

This is the output: insert into test values (' ', ',email@email.com') < lots of these

This is the script:

$fcontents = file ("file.cvs");  

  

  # expects the csv file to be in the same dir as this script 

  

  for($i=0; $i<sizeof($fcontents); $i++) {  

  

      $line = trim($fcontents[$i]);  

      

      $arr = explode("\t", $line);  

      

      $sql = "insert into test values (' ', '". implode("''", $arr) ."')";  

                  

      mysql_query($sql); 

      

      echo $sql ."<br>\n"; 

      

      if(mysql_error()) { 

      

         echo mysql_error() ."<br>\n"; 

         

      }  

}

Open in new window

0
Comment
Question by:phpretard
3 Comments
 
LVL 5

Expert Comment

by:eridanix
ID: 22784703
Can you post, how the cvs file exactly looks like?
0
 
LVL 6

Accepted Solution

by:
cr4ck3rj4ck earned 500 total points
ID: 22784713
Is it happening on every single email address?

If so, simply replace line 11 with the following:

$sql = "insert into test values (' ', '". substr(implode("''", $arr),1) ."')";

Not the most elegant of solutions, but that should drop the first character, in this case the comma, of the email address.
0
 

Author Closing Comment

by:phpretard
ID: 31509141
elegant ... smelegant!  It Works!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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.  …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

708 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