PHP Classes

PHP String Formatter: Format strings using dynamic parameters

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 62 All time: 10,454 This week: 673Up
Version License PHP version Categories
string-formatter-php 1.0MIT/X Consortium ...7.4Text processing, Templates, PHP 7
Description 

Author

This package can format strings using dynamic parameters.

It can take a string and processes it to find placeholder marks.

The package can take an array of parameters and replace the input string's placeholders with values from the array.

Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280978 in Russian Federation Russian Federation
Week rank: 47 Up6 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

string-formatter

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: 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

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}.

  Files folder image Files (12)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 files)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (12)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files (12)  /  src  
File Role Description
  Plain text file StringFormatter.php Class Class source
  Plain text file StringFormatterException.php Class Class source

  Files folder image Files (12)  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files (12)  /  tests  /  unit  
File Role Description
  Plain text file StringFormatterTest.php Class Class source

  Files folder image Files (12)  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:62
This week:0
All time:10,454
This week:673Up