如果没有机箱字符,你如何使用fgetcsv?

I'm processing csv files from geonames.org and as such I need to get csv lines without any enclosures.

Currently fgetcsv() requires that you provide a single character for the $enclosure parameter and won't process the CSV data if you don't.

How do you retrieve csv data that has no enclosure character using php native functionality?

As mentioned, the easiest thing to do is not to use fgetcsv() at all. You can just use the plain fgets() function to read each line from the CSV, and then use explode() to create an array of all the different fields per line

Here's a small example:

$handle = fopen("foo.csv", "r");

$i = 1;  // for test output

while (($buffer = fgets($handle)) !== false) {
    $fields = explode(";", $buffer);  // assuming that fields are separated with semicolons

    $j = 1;

    // Test output
    echo "Line $i: ";
    // This is just for show, you can use a regular for loop instead
    foreach ($fields as $field) {
        echo "Field $j: $field";
        $j++;
    }

    echo "
";
    $i++;
}

fclose($handle);

As a possible workaround you can use an odd character as the enclosure:

// Read TSV line using 'backspace' as the enclosure character
fgetcsv($stream, 0, "\t", chr(8));