Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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‚Ķ
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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