基于PHP读取csv文件内容的详解
本篇文章是对PHP读取csv文件的内容进行了详细的分析介绍,需要的朋友参考下
一次性读取csv文件内所有行的数据
复制代码 代码如下:
$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]."
";
}
} */
echo $goods_list[2][0];
fclose($file);
?>
读取csv文件的某一行数据
复制代码 代码如下:
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);
?>
读取csv文件制定行数(行区间)
复制代码 代码如下:
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
$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."
";
}
?>
另外从网上找的两种方法(没测试,香港虚拟主机,不知道好不好使)
复制代码 代码如下:
$handle=fopen("1.csv","r");
while(!feof($handle)){
$buffer=fgetss($handle,2048);
$data=explode(",",$buffer);
$num=count($data);
for($i=0;$iprint_r($data);
}
}
?>
复制代码 代码如下:
$handle=fopen("1.csv","r");
$row=1;
while($data=fgetcsv($handle,1000,",")){
$num=count($data);
for($i=0;$iecho $data[$i];
}
$row++;
}
?>
,美国空间

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
