filter: mysql

I don't have the original post any more, but you will have some
kind of file where the records are lines and the fileds in the
record are separated by some kind of character, say tab or blank
or anything else, but you will know what.

So let's say the filename is $filename and the separator is $sep.
Then you read the content of the file into an array with

$content = file($filename);
Now the elements of the array are the records. You will have to
convert the separator into , to get csv style. There are several
styles to do that, for example

while(list($key, $val) = each($comment)) {
   $content[$key] = str_replace($sep, ',', $val);
}

Now $content should be written to a file to feed to mysql. Well,
this is a common task, and there isn't a handy php function as
far as I know, so I wrote it myself (see below). With that
function, I don't have to think about all that mumbo jumbo and
get it right nevertheless:

stringToFile(implode("\n", $content), $filename_csv);

The implode function transforms the array to a string which can
be written to the file, glueing the rows together with \n (new
line) in this case.

function stringToFile($str, $filename, $mode="a"){
        $fp = fopen($filename,$mode);
        $res = fwrite($fp, $str);
        fclose($fp);
        return $res;
}

Of course, I have written the reverse function, too:

function fileToString($filename, $sep=' ') {
        return @implode($sep, @file($filename));
}

The @ insures that no error messages are shown. So this function
will return nothing if the file does not exist.

Kit Kerbel schrieb am Sonntag, 29. Juli 2001, 21:33:16:

> I'm the original poster of the "parsing text file into mysql database" note. 
>   I am in need of some extra assistance on this topic.  I kinda know what's 
> going on here, but just need to see it to understand it, you know?  So if 
> you could, extra assistance would be greatly appreciated.
> Thanks,
> Kit


> ----Original Message Follows----
> From: Werner Stuerenburg <[EMAIL PROTECTED]>
> Reply-To: Werner Stuerenburg <[EMAIL PROTECTED]>
> To: Kit Kerbel <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
> Subject: Re: Parsing text file into mysql database.
> Date: Sun, 29 Jul 2001 19:00:21 +0200

>  > Unfortunately I am no PHP expert but rather familiar with Perl but I 
> don't

> Why don't you convert your textfile to a csv file with
> str_replace() and then import it the normal way - both operations
> are a snap. If you need further assistance, give me a note.

> --
> Herzlich
> Werner Stuerenburg

> _________________________________________________
> ISIS Verlag, Teut 3, D-32683 Barntrup-Alverdissen
> Tel 0(049) 5224-997 407 · Fax 0(049) 5224-997 409
> http://pferdezeitung.de




> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp



-- 
Herzlich
Werner Stuerenburg            

_________________________________________________
ISIS Verlag, Teut 3, D-32683 Barntrup-Alverdissen
Tel 0(049) 5224-997 407 · Fax 0(049) 5224-997 409
http://pferdezeitung.de



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to