?
Solved

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

Posted on 2008-10-23
3
Medium Priority
?
258 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
[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 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 2000 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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

719 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