tags:

views:

204

answers:

3

In case of csv file we have fgetcsv in php to parse and get the output but in my case file is .dat and I need to parse it and store it into MySQL Database and so do we have any built in function in php like fgetcsv that can work in similar fashion on .dat file ?

Here is the sample value, it has headers DF_PARTY_ID;DF_PARTY_CODE;DF_CONNECTION_ID and its value as mentioned under.

Sample Data:

DF_PARTY_ID;DF_PARTY_CODE;DF_CONNECTION_ID

87961526;4002524;13575326
87966204;4007202;13564782
+1  A: 

IMHO .dat files can be of different formats. Blindly following the extension can be error-prone. If however you have a file from some specific application, maybe tell us what this app is. Chances are there are some parsing libraries or routines.

Marcin Cylke
+1  A: 

What's wrong with fgetcsv()? The extension on the file is irrelevant as long as the format of the data is consistent across all of your files.

Example:

$fh = fopen('example.dat', 'r');
while (!feof($fh)) {
        var_dump(fgetcsv($fh, 0, ';'));
}

Alternatively, with PHP5.3 you can also do:

$lines = file('example.dat');
foreach($lines as $line) {
        var_dump(str_getcsv(trim($line), 0, ';'));
}
Mark
A: 

I would imagine it would be easier to write a short function using fopen, fread, and fclose to parse it yourself. Read each line, explode to an array, and store them as you wish.

eykanal