Search code examples
phpcsvlinefeed

For PHP, how to read in correct lines from CSV file with LF in some CSV text field?


Input csv file (note: there's a non-printable LF character \n after fruit):

1,"apple", "水果fruit\n",300
2,"donut", "甜點dessert",200

My PHP program:

function wpa(&$arr) { echo nl2br(print_r($arr, true)); }
header("Content-Type:text/html; charset=utf-8");
$lines = file("test.csv", FILE_IGNORE_NEW_LINES);

); wpa($lines);

Output:

Array
(
[0] => 1,"水果apple", "fruit

[1] => \n",300

[2] => 2,"甜點donut", "dessert",200
)

My question:
How can I read in the csv file and properly split it into 2 csv lines other than using fgetcsv? (note: Input file has BIG5-encoded Chinese characters and fgetcsv will mess up those Chinese characters on my PHP 5.2 environment)?


Solution

  • This is not a permanent answer but it takes care my problem:

    Since the input file is edited under Windows, I write this code segments:

    $data = file_get_contents("test.csv");
    $lines = explode(PHP_EOL, $data); // or replace PHP_EOL with" \r\n"
    echo nl2br(print_r($lines,true));
    // parse with regular expression for each element in lines
    

    It outputs correct 2 csv lines. But if the input file too large to read in at one time, I don't know the answer since line(), fgets(), .etc and other read-text-file functions all treats LF as line break even if you run PHP program under Windows.