PHP operation csv file code explanation _ PHP Tutorial

Source: Internet
Author: User
PHP operation csv file code details. There are many ways to read and operate csv files in php. The simplest way is to directly open the txt file using the fopen image, and then press and separate it using explode, there are many ways to directly use php to read and operate csv files. The simplest way is to directly use fopen to open a txt file, and then use explode to press and separate the file, similarly, you can directly use a, B, c, and e to save them as KO Files in csv format. However, php also provides the fputcsv function for csv file operations. I will introduce it below.

1. generate CVS, fputcsv () format the rows as CSV and write the file pointer

The code is as follows:

$ List = array (
'AAA, bbb, ccc, dddd ',
'11, 22, 33 ',
'"Php", "java "'
);
$ Fp = fopen('file.csv ', 'w ');
Foreach ($ list as $ line ){
Fputcsv ($ fp, explode (',', $ line ));
}
Fclose ($ fp );
?>


Tips: If your csv file contains Chinese characters, you need to pay attention to Chinese characters. Otherwise, Chinese characters may be garbled with you. here is an example to solve Chinese garbled characters.

The code is as follows:

/** By www. bKjia. c0m */
$ Row = 1;
$ Handle = fopen ("file.csv", "r ");
// Fgetcsv () parses the read rows, finds fields in CSV format, and returns an array containing these fields.
While ($ data = fgetcsv ($ handle, 1000 ,",")){
$ Num = count ($ data );
Echo"

$ Num fields in line $ row:
N ";
$ Row ++;
For ($ c = 0; $ c <$ num; $ c ++ ){
// Pay attention to Chinese garbled characters
$ Data [$ c] = iconv ("gbk", "UTF-8 // IGNORE", $ data [$ c]);
Echo $ data [$ c]."
N ";
}
}
Fclose ($ handle );
?>

Reads a row of data from a csv file.

The code is as follows:

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)

The code is as follows:
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 ."
";
}
?>

And separate them to generate them directly...

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.