Re: insert *.TAB into table
| From: | Nick Talbott | Date: | Tue, 01 Aug 2000 14:17:00 +0000 |
| Subject: | Re: insert *.TAB into table | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-9446@lists.php.net to get a copy of this message | ||
fgetcsv() is designed to read text files using the Comma-Separated Value
format. In a CSV file, whitespace between fields is insignificant and is
stripped out by the C code that implements fgetcsv(). Therefore while you
can change the delimiter in fgetcsv() to get it to work with other
line-orientated non-CSV text formats (for example reading from the unix
passwd file with a '|' delimiter) it will not work using any whitespace
character such as " ", "\t", "\n" etc as a delimiter. That's a
basic
limitation of the CSV format.
Regards - Nick (contributor of the fgetcsv() code)
Nick Talbott
IT Policy and Strategy Manager, Powys County Council, UK
email nickt@powys.gov.uk
FAX +44 (0) 1597 824781
web http://www.powys.gov.uk and http://www.powysweb.co.uk
-----Original Message-----
From: Boget, Chris <Chris.Boget@wild.net>
To: 'Vadee Prudon' <v.prudon@davidson.nl>; php-general@lists.php.net
<php-general@lists.php.net>
Date: 01 August 2000 14:29
Subject: RE: [PHP] insert *.TAB into table
>> I'm using an *.tab file and importing like ;
>> $fp = fopen ("$userfile","r");
>> while ($data = fgetcsv($fp, 10000, " ")){
>
>Well, if this is a tab delimited file, then the above should
>be:
>
>while ($data = fgetcsv($fp, 10000, "\t")){
>
>More importantly, though, is that if you are having
>problems with empty fields then fgetcsv() might not
>be the way to do what you need. To determine if this
>is the case, I'd probably change the above line in your
>code to use tabs instead of spaces. If you are still
>having the problem after that, then you should probably
>change the approach entirely to accomodate the
>possibility of empty fields. To do that, you can do
>something like this:
>
>$fileArray = file( $userfile );
>for( $i = 0; $i < count( $fileArray ); $i++ ) {
> // convert the double tabs to "tab space tab"
> $curRec = eregi_replace( "\t\t", "\t \t", $fileArray[$i] );
>
> // turn this into something we can use to insert
> $curRec = eregi_replace( "\t", "\", \"", $curRec );
>
> $dbQuery = "INSERT INTO mytable
> ( ... field list ... )
> VALUES
> ( \"$curRec\" )";
> $insertResult = mysql( $dbname, $dbQuery )
>
> // do your query error checking here
>
>}
>
>
>The above is a quick and dirty example. What the
>line
>
> $curRec = eregi_replace( "\t", "\", \"", $curRec );
>
>does is turn something like
>
>name\taddress\temail\tphone\tfax
>
>into
>
>name", "address", "email", "phone", "fax
>
>which you can use directly in your insert query. All
>you need to do is add the beginning and end quotes
>(so that mySQL doesn't yell and scream), which is
>what I do in the actual query. You just need to make
>sure that all of your fields match up.
>
>Chris
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>