Several examples of PHP reading csv file content

Source: Internet
Author: User
Several examples of PHP reading csv file content

  1. $ File = fopen('windows_201511s.csv ', 'r ');
  2. While ($ data = fgetcsv ($ file) {// read the contents of a row in the CSV each time.
  3. // Print_r ($ data); // This is an array. to obtain each data, access the array subscript.
  4. $ Goods_list [] = $ data;
  5. }
  6. // Print_r ($ goods_list );
  7. /* Foreach ($ goods_list as $ arr ){
  8. If ($ arr [0]! = ""){
  9. Echo $ arr [0]."
    ";
  10. }
  11. }*/
  12. Echo $ goods_list [2] [0];
  13. Fclose ($ file );
  14. ?>

Example 2: Read a row of data in a csv file.

  1. Function get_file_line ($ file_name, $ line ){
  2. $ N = 0;
  3. $ Handle = fopen ($ file_name, 'r ');
  4. If ($ handle ){
  5. While (! Feof ($ handle )){
  6. + + $ N; // bbs.it-home.org
  7. $ Out = fgets ($ handle, 4096 );
  8. If ($ line = $ n) break;
  9. }
  10. Fclose ($ handle );
  11. }
  12. If ($ line = $ n) return $ out;
  13. Return false;
  14. }
  15. Echo get_file_line ("windows_201511s.csv", 10 );
  16. ?>

Example 3: Read the csv file to specify the number of rows (row interval ).

  1. Function get_file_line ($ file_name, $ line_star, $ line_end ){
  2. $ N = 0;
  3. $ Handle = fopen ($ file_name, "r ");
  4. If ($ handle ){
  5. While (! Feof ($ handle )){
  6. + + $ N;
  7. $ Out = fgets ($ handle, 4096 );
  8. If ($ line_star <= $ n ){
  9. $ Ling [] = $ out;
  10. }
  11. If ($ line_end = $ n) break;
  12. }
  13. Fclose ($ handle );
  14. } // Bbs.it-home.org
  15. If ($ line_end = $ n) return $ ling;
  16. Return false;
  17. }
  18. $ Aa = get_file_line ("windows_201511s.csv", 11, 20); // from 11th rows to 20th rows
  19. Foreach ($ aa as $ bb ){
  20. Echo $ bb ."
    ";
  21. }
  22. ?>

Example 4: two methods provided by netizens are not tested. Method 1,

  1. // Read the contents of the csv file
  2. $ Handle = fopen ("1.csv"," r ");
  3. While (! Feof ($ handle )){
  4. $ Buffer = fgetss ($ handle, 2048 );
  5. $ Data = explode (",", $ buffer );
  6. $ Num = count ($ data );
  7. For ($ I = 0; $ I <$ num; $ I ++ ){
  8. Print_r ($ data );
  9. }
  10. }
  11. ?>

Method 2,

  1. // Read the contents of the csv file
  2. $ Handle = fopen ("1.csv"," r ");
  3. $ Row = 1;
  4. While ($ data = fgetcsv ($ handle, 1000 ,",")){
  5. $ Num = count ($ data );
  6. For ($ I = 0; $ I <$ num; $ I ++ ){
  7. Echo $ data [$ I];
  8. }
  9. $ Row ++;
  10. }
  11. ?>

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.