Hi. I'm trying to import a text file into MySQL. I can't use the MySQL function to do it directly - I don't have access. The web host offers to do it for me, but I don't want to bug them every time I need to do an import, especially when I'm testing. (That's just a little bit of background).

So, I'm using PHP, and I've got it working just fine, sort of. The text file has the fields separated by commas and enclosed in quotes. I simply "explode" each line at the commas, then remove all quotes, and I'm good to go. However, as luck would have it, some (not a lot) of the fields for some records have a comma in them, so when I do a PHP "explode" on the commas, I've got an obvious problem. Worse, the text file is coming from a really lame proprietary database that doesn't allow the flexibility to specify a delimiter such as the pipe (|) character. It's comma-delimited and that's it.

I thought of maybe doing the explode on '"," (that's a double quote followed by a comma) since it's highly unlikely that a comma would be the first character of any particular field. That way, it would ignore the commas in the middle of fields. However, that causes me to have to remove the quotes AFTER I do the explode, and when I try to do that, it doesn't work at all. Here's some code:

$file = file("test_import1.txt");
foreach($file as $line) {
$sql .= "INSERT INTO MailList SET ";
$line = ereg_replace('"',"",$line);
$data = explode('",',$line);
etc...

The above works fine. But the following does not:

$file = file("test_import1.txt");
foreach($file as $line) {
$sql .= "INSERT INTO MailList SET ";
$data = explode('",',$line);
$data = ereg_replace('"',"",$data);
etc...

I guess it doesn't like me trying to use that function on an array like that. :-)

Any ideas? Thanks!