hikarine3/csv-parser

CSV Parser Library for PHP

v0.1.9 2018-05-19 08:14 UTC

This package is auto-updated.

Last update: 2024-12-21 00:52:49 UTC


README

Simple CSV parser for PHP. Return characters will be converted into

<br />

How to install it

composer require hikarine3/csv-parser;

at the directory where composer.json exists

Example: First line will be used as key

Let's consider csv file whose column name is id and name

$file = "input.csv";
$delimiter = ",";
$parser = new CsvParser();
$datas = $parser->parse({"delimiter" => $delimiter, file" => $file});
foreach ($datas as $data) {
    if(isset($data['id']) && isset($data['name']) ) {
/* ... */
    }
}

Default deliiter is ','

Example of using this library for Laravel's seeder

<?php

use Illuminate\Database\Seeder;
use Hikarine3\CsvParser;
use Carbon\Carbon;

class CountrySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        ini_set('memory_limit','1024M');
        $file = __DIR__ .'/data/locations/countries.tsv';
        $parser = new CsvParser();
        $datas = $parser->parse(['delimiter' => "\t", 'file' => $file]);

        foreach ($datas as $data) {
            if(isset($data['id']) && isset($data['name']) ) {
                DB::table('countries')->insert([
                    'id' => $data['id'],
                    'name' => $data['name'],
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now()
                ]);
            }
        }
    }
}

License / ライセンス / 执照

MIT

Author / 作者

Name / 名前 / 全名

Hajime Kurita

Twitter

Blog

Technical web services / 提供してる技術関連Webサービス / 技术网络服务

VPS & Infra comparison / VPS比較 / VPS比较

Programming Language Comparison / プログラミング言語比較 / 编程语言比较

OSS