0

i'm trying to import the txt file. Using fgetcsv a hole line is stored in a variable, then i imploded it, \t as pattern, bu that leads me an array with empty spaces (probably aditional tabs), so i don't know how may i get rid of this or how to do the query, as there will be elements without the needed values. I still don't know if this will be a pattern for all lines (if it's so i can just jump them when using query). Can someone help me?

    $line = fgetcsv($fileopn, "\t");
    $c = count($line);
    print("$c\n");
    $line_str = implode("\t", $line);
    print_r($line_str);
    print("\n");        
    $fields = explode("\t",$line_str);
    $c2 = count($fields);
    print("$c2\n");
    print_r($fields);

1 Answer 1

0

From pset8 instructions:

Note that fgetcsv takes an optional third argument

Read the PHP doc

length
Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower.

If you do not use the length argument, $line[0] looks like this:

[0] => string(46) "US 34050 FPO AA Erie 029 41.0375 -111.6789 "

If you use the length argument, $line[0] looks like this:

[0] => string(2) "US"

Since you don't know the length of the longest line, use 0. You won't notice the "slightly slower". If you use this syntax for fgetcsv, $line will be the array you want and you won't need any *sploding.

4
  • Yes, that solved. Now i got into another error. I'll postar another question. Thanks pal.
    – user10187
    Commented Dec 4, 2015 at 15:02
  • You're welcome buddy. Please try and get in the habit of "accepting" answers that help you out,including your own! (that green check thingy), because then the answers are more likely to help others too. True, there's rep at stake, but you know the old expression "Don't applaud, just throw money" :) Commented Dec 4, 2015 at 15:17
  • Ok, i was wondering where to vote... Thanks again.
    – user10187
    Commented Dec 4, 2015 at 15:55
  • You're welcome again and Thanks back! Commented Dec 4, 2015 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .