Solved

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

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to dynamically set the form action using jQuery.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

627 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