Solved

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

Posted on 2008-10-23
3
255 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Go Fund Me" type plugin 5 66
Alert after MVC form submitted 1 41
Please explain purpose of GZIP 4 83
Exchange OWA website Redirection 7 81
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
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 learn how to dynamically set the form action using jQuery.

752 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