php fgetcsv讀取csv檔案代碼(1/2)

來源:互聯網
上載者:User
 代碼如下 複製代碼

function get_csv_contents( $file_target ){

    $handle  = fopen( $file_target, 'r');

    while ($data = fgetcsv($handle, 1000, ",")) {
   
        $num = count($data);
        echo "<p> $num fields in line $row: <br> ";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c]. "<br> ";;
            /*echo getUTFString($data[$c])*/
        }
    }

    fclose($handle);
}

array fgetcsv ( int handle [, int length [, string delimiter [, string enclosure]]] )

 


handle
一個由 fopen()、popen() 或 fsockopen() 產生的有效檔案指標。

length (可選)
必須大於 CVS 檔案內最長的一行。在 PHP 5 中該參數是可選的。如果忽略(在 PHP 5.0.4 以後的版本中設為 0)該參數的話,那麼長度就沒有限制,不過可能會影響執行效率。

delimiter (可選)
設定欄位分界符(只允許一個字元),預設值為逗號。

enclosure (可選)
設定欄位環繞符(只允許一個字元),預設值為雙引號。該參數是在 PHP 4.3.0 中添加的。


和 fgets() 類似,只除了 fgetcsv() 解析讀入的行並找出 CSV 格式的欄位然後返回一個包含這些欄位的數組。

fgetcsv() 出錯時返回 FALSE,包括碰到檔案結束時。

 

首頁 1 2 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.