承接 mattmezza/line-map 相关项目开发

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

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

mattmezza/line-map

Composer 安装命令:

composer require mattmezza/line-map

包简介

Do stuff for each line of text

README 文档

README

Packagist PHP from Packagist GitHub license

Maps a function through the lines of a file, line by line

Install it with composer require mattmezza/line-map

Use it like this:

$map = Map\CSV::fileNamed("example.csv");
$rows = $map->with(function ($row, $idx) {
    return array_merge([$idx + 1], $row);
})->get();
$header = $map->getHeader();

or

Map\Txt::fileNamed("lines.txt")->with(function ($line, $idx) {
    echo "$idx: $line";
});

TXT

// the callback function - can be also an anonymous closure
$doStuff = function($line, $lineNumber) {
    return $lineNumber . ": " . $line;
};

// from a string variable
$text = "line 1
line2
line3";
$newLines = Map\Txt::string($text)->with($doStuff)->toArray();

// from a filename
$filename = "./somelines.txt";
$newLines = Map\Txt::fileNamed($filename)->with($doStuff)->toArray();

// from a file resource handle
$file = fopen($filename, "r");
$newLines = Map\Txt::file($file)->with($doStuff)->toArray();

CSV

// the callback function - can be also an anonymous closure
$doStuff = function($row, $lineNumber, $headers) {
    echo $row[$headers[0]];
    return $row;
};

// from a string variable
$csv = "name,surname
John,Doe
Foo,Bar";
$newRows = Map\CSV::string($csv)->with($doStuff)->toArray();

// from a filename
$filename = "./somedata.csv";
$newRows = Map\CSV::fileNamed($filename)->with($doStuff)->toArray();

// from a file resource handle
$file = fopen($filename, "r");
$newRows = Map\CSV::file($file)->with($doStuff)->toArray();
example

Rapidly cycle through this CSV

name,username,email
Matt,mattmezza,mattmezza@gmail.com

for each line generate custom message replacing values using the following template

Hello {{name}},
your username is {{username}}.

send each message to the right user and collect some logs

$tpl = "Hello __name__,
your username is __username__.";
$logs = Map\CSV::fileNamed("file.csv")->with(function($row, $idx, $headers) use ($tpl) {
    $msg = $tpl;
    foreach ($headers as $header) {
        $msg = str_replace("{{".$header."}}", $row[$header], $msg);
    }
    return sendMail($row["email"]);
})->toArray();
Matteo Merola mattmezza@gmail.com

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固