Home >Backend Development >PHP Tutorial >Detailed explanation of PHP operation csv file code_PHP tutorial

Detailed explanation of PHP operation csv file code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:06:28878browse

There are many ways to read and operate csv files in php. The simplest one can directly use fopen to open a txt file, and then use explode to separate it. The same is true for generation, directly use a, b, c, It is KO to save e into csv format, but php also provides the fputcsv function to operate csv files. I will introduce it below.

1. Generate CVS, fputcsv() formats the rows into CSV and writes 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);
?>

Copy code


代码如下 复制代码

/** by www.bKjia.c0m */
$row = 1;
$handle = fopen("file.csv","r");
//fgetcsv() 解析读入的行并找出 CSV格式的字段然后返回一个包含这些字段的数组。
while ($data = fgetcsv($handle, 1000, ",")) {
$num = count($data);
echo "

$num fields in line $row:
n";
    $row++;
    for ($c=0; $c < $num; $c++) {
  //注意中文乱码问题
  $data[$c]=iconv("gbk", "utf-8//IGNORE",$data[$c]); 
        echo $data[$c] . "
n";
    }
}
fclose($handle);
?>

$list = array (

'aaa,bbb,ccc,dddd',
 代码如下 复制代码

 

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);
?>

'11,22,33',

'"php","java"'
);

$fp = fopen('file.csv', 'w');

foreach ($list as $line) {

fputcsv($fp, explode(',', $line));
 代码如下 复制代码
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."
";
}
?>
} fclose($fp); ?> A small tip, if there is Chinese in your csv, we need to pay attention to the Chinese problem, otherwise the Chinese garbled characters will play with you. Here is an example to solve the Chinese garbled characters.
The code is as follows Copy code
/**by www.bKjia.c0m*/<🎜> $row = 1;<🎜> $handle = fopen("file.csv","r");<🎜> //fgetcsv() parses the read line and finds the fields in CSV format and returns an array containing those 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 the problem of Chinese garbled characters $data[$c]=iconv("gbk", "utf-8//IGNORE",$data[$c]); echo $data[$c] . "
n"; } } fclose($handle); ?> Read a certain row of data from the csv file

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630703.htmlTechArticleThere are many ways to read and operate csv files in php. The simplest one can directly use fopen to open txt files. Open it, then use explode to separate it, and use it directly to generate...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn