2

I want to read a file in PHP, the structure of file is as follows:

text1,text2,text3,.................

...................................

...................................

...................................

...................................

WORD;

NUMBER

col1; col2; col3; [CSV part of the file starts from here]

value1; value2; value3

I want to read CSV part of the file and inset it into MySql database. I looked for fgetCsv but unable to do so. What is the way to do it?

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • Can you define the format of the file? Or do you get the file in this format? – René Höhle Sep 11 '12 at 07:40
  • No, I don't have the control over the format of the file. –  Sep 11 '12 at 07:44
  • there are same number of lines and they end with `WORD;` and `NUMBER` ? – Mihai Iorga Sep 11 '12 at 07:44
  • Number of lines may vary but csv part of the file will always start just after NUMBER –  Sep 11 '12 at 07:45
  • Its easy when you have all data in one line devided with the \r after the content block. – René Höhle Sep 11 '12 at 07:47
  • Yes, but as I said I don't have control over the structure of the file. –  Sep 11 '12 at 07:49
  • All of the lines above are more or less CSV format. That shouldn't pose an issue for fgetcsv at all. It's up to you to test whether the desired portion of data has started. – Gordon Sep 11 '12 at 07:49
  • I'm sorry that I haven't written explained scenario well. But the text above is random. –  Sep 11 '12 at 07:52

2 Answers2

0

I had a similar file, that I needed to parse and write to a couple of columns. I decided to read the file, line by line, using fgetcsv:

exec('wc -L file.tmp',$res,$stat);
$length = (int)($stat === 0 ? $res[0] : 200);
$fh = fopen('file.tmp','r');
$fields = false;
while($line = fgetcsv($fh,$length,';'))
{
    if (count($line) <= 1)//not more than one semi-colon
    {
        continue;//not part of csv-section, read on
    }
    if (!$fields)
    {
        $fields = $line;//<-- first line of CSV === columns
        continue;
    }
    //$line should be what you need here.
}

Now, in my case, I decided to write the results to a new csv-file, and use LOAD LOCAL INFILE. You can read here how that panned out ;-)

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Why don't you try to read the last line of the file and count the number of delimiters. After that read each line of the file until you encounter a line with the appropriate number of delimiters. Once this happens create a new string starting from that line onwards and parse it as csv.

$arr = @file('foo.php');
$lastLine = $arr[count($arr)-1];
$delimiterCount = substr_count($lastLine,";");
$csvStarted=false;
$csvString="";
foreach($line in $arr) {
    if($csvStarted) {
        $csvString += $line + "\r\n";
    } else {
        if(substr_count($line,";")==$delimiterCount) {
            $csvStarted=true;
            $csvString += $line + "\r\n";
        }
    }
}

Code is not efficient, but gives you an idea. Once u find the start of the CSV part, you can break the loop and implode the rest of the array.

tmazur
  • 173
  • 6