htmlburger/carbon-csv 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

htmlburger/carbon-csv

Composer 安装命令:

composer require htmlburger/carbon-csv

包简介

Simple CSV file parser

README 文档

README

Carbon CSV is a PHP library aimed at simplifying CSV parsing.

It provides simple interface to ease mapping columns via a header row, or custom column names.

Installation

composer require htmlburger/carbon-csv

Usage

Suppose that you have the following CSV:

First Name Last Name Company Name Address
Homer Simpson Springfield Nuclear Power Plant 742 Evergreen Terrace, Springfield
Ned Flanders The Leftorium 744 Evergreen Terrace, Springfield

Here is how you could iterate through the rows:

use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage()); 
}

Would produce the following output:

Array
(
    [First Name] => Homer
    [Last Name] => Simpson
    [Company Name] => Springfield Nuclear Power Plant
    [Address] => 742 Evergreen Terrace, Springfield
)
Array
(
    [First Name] => Ned
    [Last Name] => Flanders
    [Company Name] => The Leftorium
    [Address] => 744 Evergreen Terrace, Springfield
)

Alternatively, you could also provide your own column names:

use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();
    $csv->set_column_names([
        'First Name'   => 'fname',
        'Last Name'    => 'lname',
        'Company Name' => 'company',
        'Address'      => 'address',
    ]);

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage()); 
}

Would produce the following output:

Array
(
    [fname] => Homer
    [lname] => Simpson
    [company] => Springfield Nuclear Power Plant
    [address] => 742 Evergreen Terrace, Springfield
)
Array
(
    [fname] => Ned
    [lname] => Flanders
    [company] => The Leftorium
    [address] => 744 Evergreen Terrace, Springfield
)

MacOS encoding

When working with files created on a Mac device, you should set the auto_detect_line_endings PHP variable to 1.

ini_set( 'auto_detect_line_endings', 1 );

Settings

To change the delimiter, enclosure and escape characters for the CSV file, simply pass them as arguments after the file path.

Example:

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv', ';', '|', '/');
$rows = $csv->to_array();

Methods

Methods for skipping rows or columns work with zero based indexes.

skip_to_row(int $row_index)

To skip to a specific row, simply pass the index of the row.

This will tell the parser to start reading from that row until the end of the file.

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_row(1);
$rows = $csv->to_array();

Contents before skipping to a specific row:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

Contents after skipping to a specific row:

Array
(
    [0] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

skip_to_column(int $col_index)

To skip to a specific column, simply pass the index of the column.

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_column(2);
$rows = $csv->to_array();

Contents before skipping to a specific column:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

Contents after skipping to a specific column:

Array
(
    [0] => Array
        (
            [0] => Simple Company Name
            [1] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Nice Company Name
            [1] => Street Name, 5678, City Name, Country Name
        )
)

skip_columns(array $col_indexes)

To skip multiple columns, pass the indexes of those columns as an array.

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_columns(array(0, 2, 3));
$rows = $csv->to_array();

Contents before skipping columns:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

Contents after skipping columns:

Array
(
    [0] => Array
        (
            [0] => Doe
        )
    [1] => Array
        (
            [0] => Doe
        )
)

use_first_row_as_header()

To use the first row from the CSV, simply call this method.

Note: if skip_to_row is called prior to calling use_first_row_as_header, the parser will use the new first row as a header.

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->use_first_row_as_header();
$rows = $csv->to_array();

Contents before assigning a header row:

Array
(
    [0] => Array
        (
            [0] => First Name
            [1] => Last Name
        )
    [1] => Array
        (
            [0] => John
            [1] => Doe
        )
    [2] => Array
        (
            [0] => Jane
            [1] => Dove
        )
)

Contents after assigning a header row:

Array
(
    [0] => Array
        (
            [First Name] => John
            [Last Name] => Doe
        )
    [1] => Array
        (
            [First Name] => Jane
            [Last Name] => Dove
        )
)

Since we're telling the parser to use the first row as a header row, it is assigned and skipped.

set_column_names(array $columns_mapping)

If you wish to use your own indexes for the columns, pass them using an array.

Note: you can use set_column_names in conjunction with use_first_row_as_header, so you can set the names of the columns based on the header row.

Example without use_first_row_as_header (using a file without a head row):

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->set_column_names([
    0 => 'first_name',
    1 => 'last_name',
    2 => 'company_name',
    3 => 'address',
]);
$rows = $csv->to_array();

Contents before setting custom column names:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

Contents after setting custom column names:

Array
(
    [0] => Array
        (
            [first_name] => John
            [last_name] => Doe
            [company_name] => Simple Company Name
            [address] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [first_name] => Jane
            [last_name] => Doe
            [company_name] => Nice Company Name
            [address] => Street Name, 5678, City Name, Country Name
        )
)

Example with use_first_row_as_header (using a file with a head row):

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->use_first_row_as_header();
$csv->set_column_names([
    'First Name' => 'first_name',
    'Last Name' => 'last_name',
    'Company Name' => 'company_name',
    'Address' => 'address',
]);
$rows = $csv->to_array();

Contents before setting custom column names:

Array
(
    [0] => Array
        (
            [0] => First Name
            [1] => Last Name
            [2] => Company Name
            [3] => Address
        )
    [1] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [2] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

Contents after setting custom column names:

Array
(
    [0] => Array
        (
            [first_name] => John
            [last_name] => Doe
            [company_name] => Simple Company Name
            [address] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [first_name] => Jane
            [last_name] => Doe
            [company_name] => Nice Company Name
            [address] => Street Name, 5678, City Name, Country Name
        )
)

set_encoding($encoding)

Set the encoding of the CSV file. This is needed, so it can be properly converted to utf-8

Example:

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->set_encoding('windows-1251');
$total_number_of_rows = $csv->count();

count()

Get the total number of rows in the CSV file (this skips the empty rows):

use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$total_number_of_rows = $csv->count();

$total_number_of_rows = $csv->count() is equivalent to count($csv->to_array()).

htmlburger/carbon-csv 适用场景与选型建议

htmlburger/carbon-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.05k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 htmlburger/carbon-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 htmlburger/carbon-csv 我们能提供哪些服务?
定制开发 / 二次开发

基于 htmlburger/carbon-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 41.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 6
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-09-08