csv fileIntroduction:
CSV is a versatile, relatively simple file format that is widely used by users, business, and science. The most widespread application is for transferring tabular data between programs that themselves operate on incompatible formats (often proprietary and/or non-canonical formats). Because a large number of programs support some variant of CSV, at least as an optional input/output format.
For example, a user may need to exchange information from a database program that stores data in a proprietary format to a spreadsheet with data in a completely different format. Most likely, the database program can export the data as "CSV" and the exported CSV file can then be imported by a spreadsheet program.
"CSV" is not a single, well-defined format (although RFC 4180 has a commonly used definition). In practice, therefore, the term "CSV" refers broadly to any file that is:
plain text, using a certain character set, such as ASCII, Unicode, EBCDIC or GB2312;
consists of records (typically one record per line);
Each record is separated into fields by a delimiter (Typical delimiters are commas, semicolons, or tabs; sometimes delimiters can include optional spaces);
Each record has the same field sequence.
Under these general constraints conditions, there are many CSV variants, so CSV files are not completely interoperable. However, these variations are very minor, and there are many applications that allow the user to preview the file (which is possible since it is plain text) and then specify delimiters, escaping rules, etc. If the variation in a particular CSV file is too large to be supported by a particular receiving program, then the feasible approach is often to manually inspect and edit the file, or to fix the problem through a simple program. Therefore, in practice, CSV files are still very convenient.
This article is a detailed analysis and introduction to the content of PHP reading csv files. Friends who need it can refer to it
Read all rows of data in the csv file at one time
<?php $file = fopen('windows_2011_s.csv','r'); while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容 //print_r($data); //此为一个数组,要获得每一个数据,访问数组下标即可 $goods_list[] = $data; } //print_r($goods_list); /* foreach ($goods_list as $arr){ if ($arr[0]!=""){ echo $arr[0]."<br>"; } } */ echo $goods_list[2][0]; fclose($file); ?>
Read a certain line of data in the csv file
<?php function get_file_line( $file_name, $line ){ $n = 0; $handle = fopen($file_name,'r'); if ($handle) { while (!feof($handle)) { ++$n; $out = fgets($handle, 4096); if($line==$n) break; } fclose($handle); } if( $line==$n) return $out; return false; } echo get_file_line("windows_2011_s.csv", 10); ?>
Read the csv file to specify the number of lines (line interval)
<?php function get_file_line( $file_name, $line_star, $line_end){ $n = 0; $handle = fopen($file_name,"r"); if ($handle) { while (!feof($handle)) { ++$n; $out = fgets($handle, 4096); if($line_star <= $n){ $ling[] = $out; } if ($line_end == $n) break; } fclose($handle); } if( $line_end==$n) return $ling; return false; } $aa = get_file_line("windows_2011_s.csv", 11, 20); //从第11行到第20行 foreach ($aa as $bb){ echo $bb."<br>"; } ?>
The above is the detailed content of Detailed explanation of reading csv file content in php. For more information, please follow other related articles on the PHP Chinese website!