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: 3 months 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 (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T21:18:29.000Z (7 months ago)
- Last Synced: 2025-03-24T08:18:26.300Z (4 months ago)
- Topics: formatter, helper, php, string-manipulation
- Language: PHP
- Homepage:
- Size: 13.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

[](https://scrutinizer-ci.com/g/Smoren/string-formatter-php/?branch=master)
[](https://coveralls.io/github/Smoren/string-formatter-php?branch=master)

[](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}.
```