定制 struktal/struktal-csv-reader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

struktal/struktal-csv-reader

最新稳定版本:1.1.0

Composer 安装命令:

composer require struktal/struktal-csv-reader

包简介

CSV reader library for PHP

README 文档

README

This is a PHP library to easily read CSV files.

Installation

To install this library, include it in your project using composer:

composer require struktal/struktal-csv-reader

Usage

Read all entries from a CSV file

To read all entries from a CSV file, use the following code:

$csvReader = new CSVReader();

// CSV Reader Options
$csvReader->setFile("path/to/file.csv")
          ->setHeader(false)
          ->setDelimiter(";")
          ->setMaxLineLength(null)
          ->read();

// Get the CSV Data
$data = $csvReader->getData();

You can use the setHeader(bool $header) method to specify whether the CSV file contains a header or not. If the method is called with true as parameter, the first row will be skipped and not returned in the $data array. By default, no header is assumed.

Instead of explicitly setting the delimiter, you can also use the detectDelimiter() method. This method uses the first line of the CSV file to detect which character of ,, ;, \t or | occurs most often and uses it as the delimiter.

Warning: If the first line of the CSV file contains ,, ;, \t or | more often than the actual delimiter, the method will not detect the correct delimiter.

As an example, if the CSV file looks like this:

name;age;city
Alice;25;New York
Bob;30;London
Charlie;20;Berlin
David;35;Paris
Frank;40;Tokyo

the returned $data array would be:

[
    [
        "name",
        "age",
        "city"
    ],
    [
        "Alice",
        "25",
        "New York"
    ],
    [
        "Bob",
        "30",
        "London"
    ],
    [
        "Charlie",
        "20",
        "Berlin"
    ],
    [
        "David",
        "35",
        "Paris"
    ],
    [
        "Frank",
        "40",
        "Tokyo"
    ]
]

If you'd call the setHeader(bool $header) method with true as parameter, the contents of the first row will be omitted from the $data array:

[
    [
        "Alice",
        "25",
        "New York"
    ],
    [
        "Bob",
        "30",
        "London"
    ],
    [
        "Charlie",
        "20",
        "Berlin"
    ],
    [
        "David",
        "35",
        "Paris"
    ],
    [
        "Frank",
        "40",
        "Tokyo"
    ]
]

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-16