PHP reads csv file content

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to access the contents of the csv file read by PHP. Code for reading data from all rows in the csv file at one time :? Php?filefopen(windows_2011_s.csv, r); while ($ datafgetcsv ($ file) {print_r reads a line of content in the CSV each time.

Welcome to the Linux community forum and interact with 2 million technical staff> go to PHP to read the contents of the csv file. Code for reading data from all rows in the csv file at one time :? Php $ file = fopen('windows_2011_s.csv ', 'R'); while ($ data = fgetcsv ($ file) {// read a line of content in the CSV each time // print_r

Welcome to the Linux community forum and interact with 2 million technicians>

PHP reads the contents of the csv file.

Reads data from all rows in a csv file at a time.

Code:

  

$ File = fopen('windows_201511s.csv ', 'R ');

While ($ data = fgetcsv ($ file) {// read the contents of a row in the CSV each time.

// Print_r ($ data); // This is an array. To obtain each data, access the array subscript.

$ Goods_list [] = $ data;

}//

// Print_r ($ goods_list );

/* Foreach ($ goods_list as $ arr ){

If ($ arr [0]! = ""){

Echo $ arr [0]."
";

}

}*/

Echo $ goods_list [2] [0];

Fclose ($ file );

?>

Reads a row of data from a csv file.

  

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_201511s.csv", 10 );

?>

Read csv files to specify the number of rows (row interval)

Code:

  

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_201511s.csv", 11, 20); // from 11th rows to 20th rows

Foreach ($ aa as $ bb ){

Echo $ bb ."
";

}

?>

In addition, you can find two methods on the Internet (I don't know whether to enable them)

[1] [2]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.