Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smoren/string-formatter-php
Helper for formatting strings with dynamic data
https://github.com/smoren/string-formatter-php
formatter helper php string-manipulation
Last synced: about 1 month ago
JSON representation
Helper for formatting strings with dynamic data
- Host: GitHub
- URL: https://github.com/smoren/string-formatter-php
- Owner: Smoren
- License: mit
- Created: 2022-11-27T12:52:05.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-06-15T19:25:11.000Z (over 1 year ago)
- Last Synced: 2024-11-20T07:28:21.991Z (about 2 months ago)
- Topics: formatter, helper, php, string-manipulation
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# string-formatter
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/string-formatter)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/string-formatter-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/string-formatter-php/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/Smoren/string-formatter-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/string-formatter-php?branch=master)
![Build and test](https://github.com/Smoren/string-formatter-php/actions/workflows/test_master.yml/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)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
```php
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
```php
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
```php
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
```php
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}.
```