How to extract data from csv file in PHP

前端 未结 11 1471
醉酒成梦
醉酒成梦 2020-11-22 07:36

I have a csv file which looks like this

$lines[0] = \"text, with commas\", \"another text\", 123, \"text\",5;
$lines[1] = \"some without commas\", \"another          


        
11条回答
  •  不要未来只要你来
    2020-11-22 08:27

    You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.

    Example from PHP Manual:

    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "

    $num fields in line $row:

    \n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "
    \n"; } } fclose($handle); }

提交回复
热议问题