PHP CSV file writing and reading

1. What is csv?

Comma-Separated Values ​​(CSV), its file stores tabular data (numbers and text) in plain text, and each line of the file is a data record. Each record consists of one or more fields, separated by commas.

It can be opened using WPS, the effect is the same as Excel. Because it is only an agreed format, parsing will not be as troublesome and memory-consuming as Excel.

I usually use the csv file as the export of the database content, and then open it in WPS and save it as Excel to the demander, because writing and reading Excel will be more troublesome.

2. Write csv file

  1. General content ,segmentation \r\nwrap.

    <?php
    
    $data = [
        ['id' => 1, 'name' => '哪吒', 'description' => "魔丸降生,陈塘关三太子"],
        ['id' => 2, 'name' => '敖丙', 'description' => "龙族的希望"],
        ['id' => 3, 'name' => '李靖', 'description' => "哪吒是我儿"],
    ];
    
    $file = fopen('/tmp/test.csv', 'w');
    
    foreach($data as $datum) {
          
          
    	// 判断
        fwrite($file, implode(',', $datum) . "\r\n");
    }
    
    fclose($file);
    

    File opening and WPS opening
    Insert picture description here

  2. Any line breaks and \nEnglish commas in the content ,will interfere with the parsing of csv, such as turning an array into

    $data = [
        ['id' => 1, 'name' => '哪吒', 'description' => "魔丸降生,陈塘关三太子"],
        ['id' => 2, 'name' => '敖丙', 'description' => "龙族的\n希望"],
        ['id' => 3, 'name' => '李靖', 'description' => "哪吒是我儿"],
    ];
    

    Insert picture description here
    Solution-Use field surrounding characters , the default is double quotation marks

    <?php
    
    $data = [
        ['id' => 1, 'name' => '哪吒', 'description' => "魔丸降生,陈塘关三太子"],
        ['id' => 2, 'name' => '敖丙', 'description' => "龙族的\n希望"],
        ['id' => 3, 'name' => '李靖', 'description' => "哪吒是我儿"],
    ];
    
    $file = fopen('/tmp/test.csv', 'w');
    
    foreach($data as $datum) {
          
          
        fwrite($file, '"' . implode('","', $datum) . '"' . "\r\n");
    }
    
    fclose($file);
    

    File opening and WPS opening
    Insert picture description here

  3. There are double quotes in the content. "
    Uh, I didn't think of a good way for the time being. Replace str_replace with double quotes in Chinese.

3. Read csv

  1. There is a special function in PHP to fgetcsv()parse the row of the current pointer into an array.

    <?php
    
    $file = fopen('/tmp/test.csv', 'r');
    
    while($row = fgetcsv($file)) {
          
          
    
    	var_dump($row);
    }
    

    When the pointer reaches the end of the file, return and falseexit.

  2. But it should be noted that if there is a blank line in the middle or at the end of the file, it $rowwill be parsed as an nullarray containing one element [null].

    rewrite

    <?php
    
    $file = fopen('/tmp/test.csv', 'r');
    
    while($row = fgetcsv($file)) {
          
          
    	if ($row[0] === null) continue; 
    	
    	var_dump($row);
    }
    
    fclose($file);
    
  3. Just now we said that the presence of English commas in the content ,will affect the analysis, or the use of field surround characters in the content "will also affect the resolution. In fact, it fgetcsvis possible to specify the parameters of the field separator and the field surrounding character.
    Insert picture description here
    We won't talk about the second parameter length. I don't know how much efficiency it affects, but there are two more steps to parse each line.
    The third parameter is the field separator, which is the default ,.
    The fourth parameter is the field wrapper, which is the default ". So what just said in 2.3, when you don’t need to open WPS (only read by yourself), in addition to replacing the content, you can also write the field surround character as something else when writing, and then pass in the symbol parameter when parsing .

Guess you like

Origin blog.csdn.net/z772532526/article/details/101107948