smoren/string-formatter
最新稳定版本:v2.0.0
Composer 安装命令:
composer require smoren/string-formatter
包简介
Helper for formatting strings
README 文档
README
Helper for formatting strings with dynamic data
How to install to your project
composer require smoren/string-formatter
Unit testing
composer install
./vendor/bin/codecept build
./vendor/bin/codecept run unit tests/unit
Usage
Basic usage
use Smoren\StringFormatter\StringFormatter; $input = 'Hello, {name}! Your position is {position}.'; $params = ['name' => 'Anna', 'position' => 'programmer']; $result = StringFormatter::format($input, $params); echo $result; // Hello, Anna! Your position is programmer.
Usage with nested params
use Smoren\StringFormatter\StringFormatter; $input = 'Hello, {name}! Your position is {job.position}.'; $params = ['name' => 'Anna', 'job' => ['position' => 'programmer', 'salary' => 2000]]; $result = StringFormatter::format($input, $params); echo $result; // Hello, Anna! Your position is programmer.
Custom regexp usage
use Smoren\StringFormatter\StringFormatter; $input = 'Hello, %name%! Your position is %position%.'; $params = ['name' => 'Anna', 'position' => 'programmer']; $result = StringFormatter::format($input, $params, false, '/%([a-z]+)%/'); echo $result; // Hello, Anna! Your position is programmer.
Errors handling
use Smoren\StringFormatter\StringFormatter; use Smoren\StringFormatter\StringFormatterException; // Explicit mode $input = 'Hello, {name}! Your position is {position}.'; $params = ['name' => 'Anna']; try { StringFormatter::format($input, $params); } catch(StringFormatterException $e) { print_r($e->getData()); // ['position'] } // Silent mode $input = 'Hello, {name}! Your position is {position}.'; $params = ['name' => 'Anna']; $result = StringFormatter::format($input, $params, true); echo $result; // Hello, Anna! Your position is {position}. // Another variant of silent mode $input = 'Hello, {name}! Your position is {position}.'; $params = ['name' => 'Anna']; $result = StringFormatter::formatSilent($input, $params); echo $result; // Hello, Anna! Your position is {position}.
统计信息
- 总下载量: 313
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-27