承接 cfv1000/csv-reader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cfv1000/csv-reader

最新稳定版本:2.0.4

Composer 安装命令:

composer require cfv1000/csv-reader

包简介

Another PHP csv reader

README 文档

README

Tests Latest Stable Version PHP Version Require Total Downloads Monthly Downloads License PHPUnit Rector PSR-4

🚀 Why Use This Class?

This class is a solid choice for several reasons:

Memory Efficiency: Unlike methods that read the entire file into an array, this class implements the Iterator interface. This means it reads the file line by line, which is crucial for processing large datasets without exhausting your server's memory. This is particularly useful in environments with limited RAM or when dealing with multi-gigabyte files.

Flexibility: It allows you to customize the core parameters of your CSV file. You can specify different column, line, and enclosure characters in the constructor. This adaptability makes it suitable for reading CSV files from various sources that may use different delimiters.

Countable and Iterator Interfaces: By implementing the Countable interface, you can easily get the total number of lines in the file using the count() method. The Iterator interface implementation means you can use the class directly in foreach loops, providing a clean and familiar way to process each line of the CSV.

Low-Level Control: The class gives you fine-grained control over the file pointer with methods like seek() and tell(), which can be useful for advanced operations like resuming a process or navigating to a specific part of the file.

SplFileObject Utilization: The class leverages PHP's built-in SplFileObject which is part of the Standard PHP Library (SPL). SplFileObject is an object-oriented interface for file handling, offering a more robust and feature-rich way to interact with files than traditional functions like fopen() and fgetcsv(). This ensures stable and reliable file operations.

✍️ Usage Examples

Basic Usage To read a basic comma-separated file, just provide the file path:

use cfv1000\CsvReader\Reader;

$csv = new Reader('path/to/your/file.csv');

foreach ($csv as $line) {
    print_r($line);
}

Customizing Delimiters

If your file uses a different delimiter, like a semicolon, you can specify it:

use cfv1000\CsvReader\Reader;

// Semicolon-separated file
$csv = new Reader('path/to/your/semicolon_file.csv', ';');

foreach ($csv as $line) {
    print_r($line);
}

Counting Lines

You can easily get the total line count using the count() method.

use cfv1000\CsvReader\Reader;

$csv = new Reader('path/to/your/file.csv');

$lineCount = count($csv); // This will return the total number of lines
echo "Total lines: " . $lineCount;

Note: Calling count() will move the file pointer to the end of the file. If you need to loop through the file again after counting, you must call the rewind() method first.

use cfv1000\CsvReader\Reader;

$csv = new Reader('path/to/your/file.csv');

$lineCount = count($csv); 
echo "Total lines: " . $lineCount . PHP_EOL;
// Rewind the file pointer to the beginning to start reading
$csv->rewind();

foreach ($csv as $line) {
    print_r($line);
}

统计信息

  • 总下载量: 543
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固