PHPackages                             stk2k/string-format - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stk2k/string-format

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stk2k/string-format
===================

String formatter(C# String.Format/sprintf)

0.1.2(4y ago)04022MITPHPPHP &gt;=7.2

Since Jun 9Pushed 4y ago1 watchersCompare

[ Source](https://github.com/stk2k/string-format)[ Packagist](https://packagist.org/packages/stk2k/string-format)[ Docs](https://github.com/stk2k/string-format)[ RSS](/packages/stk2k-string-format/feed)WikiDiscussions main Synced 6d ago

READMEChangelogDependencies (2)Versions (4)Used By (2)

String formatter(C# String.Format/sprintf)
==========================================

[](#string-formatterc-stringformatsprintf)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7edcdbfc4aa3fc911e5890822eba56200de08733691a237e0111238964ba4c06/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746b326b2f737472696e672d666f726d61742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/string-format)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d18467b2b7c2f308097cb4935cf9e3394534a93810b4cd7d7701e26f18bdece1/68747470733a2f2f6170692e7472617669732d63692e636f6d2f73746b326b2f737472696e672d666f726d61742e7376673f6272616e63683d6d61696e)](https://api.travis-ci.com/stk2k/string-format.svg?branch=main)[![Coverage Status](https://camo.githubusercontent.com/ddd89a8bfd4d1c319bf11e63e754c11560a7317d9d5f38d5c4b8e514f134102f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73746b326b2f737472696e672d666f726d61742f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/repos/github/stk2k/string-format/badge.svg?branch=main)[![Code Climate](https://camo.githubusercontent.com/c4e0c86de13fe53b9dc53cabb5e6211d416ac115c55c2687f3faf0052147cecf/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f73746b326b2f737472696e672d666f726d61742f6261646765732f6770612e737667)](https://codeclimate.com/github/stk2k/string-format)[![Total Downloads](https://camo.githubusercontent.com/6daca6af4c7bc67f486404dded666bb24cf7f72e9266f02d883706e0a0b38ed2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746b326b2f737472696e672d666f726d61742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/string-format)

Description
-----------

[](#description)

String formatter(C# String.Format/sprintf)

Feature
-------

[](#feature)

- Supports PHP sprintf formatter and C# String.Format formatter
- You can implement your own formatter(StringFormatterInterface)
- Implemented as a static method in trait

Usage
-----

[](#usage)

### PHP sprintf formatter

[](#php-sprintf-formatter)

```
use stk2k\string\format\test\classes\MyStringClass;
use stk2k\string\format\formatter\PhpSprintfStringFormatter;

MyStringClass::setStringFormatter(new PhpSprintfStringFormatter());

// string
echo MyStringClass::format('Hello, %s!', 'David');       // Hello, David!

// integer
echo MyStringClass::format('Come here after %d days', 3);       // Come here after 3 days
echo MyStringClass::format('%04d-%02d-%02d', 2021, 6, 10);      // 2021-06-10

// float
echo MyStringClass::format('%.03f', 3.1414926535);      // 3.141

// exponent
echo MyStringClass::format('%.3e', 362525200);      // 3.625e+8
echo MyStringClass::format('%.3E', 362525200);      // 3.625E+8
```

### C# String.Format formatter

[](#c-stringformat-formatter)

```
use stk2k\string\format\test\classes\MyStringClass;
use stk2k\string\format\formatter\PhpSprintfStringFormatter;

// string
echo MyStringClass::format('Hello, {0}!', 'David');       // Hello, David!

// integer
echo MyStringClass::format('Come here after {0:d} days', 3);       // Come here after 3 days
echo MyStringClass::format('{0:d4}-{1:d2}-{2:d2}', 2021, 6, 10);      // 2021-06-10

// float
echo MyStringClass::format('{0:F3}', 3.1414926535);      // 3.141

// exponent
echo MyStringClass::format('{0:e3}', 362525200);      // 3.625e+8
echo MyStringClass::format('{0:E3}', 362525200);      // 3.625E+8

// number
echo MyStringClass::format('{0:n}', 123456.789);      // 123,456.79
echo MyStringClass::format('{0:n3}', 123456.789);     // 123,456.789
echo MyStringClass::format('{0:n4}', 123456.789);     // 123,456.7890

// percentage
echo MyStringClass::format('Ratio: {0:P}', 0.18);      // Ratio: 18.00%
echo MyStringClass::format('Ratio: {0:P3}', 0.18);     // Ratio: 18.000%
echo MyStringClass::format('Ratio: {0:P4}', 0.18);     // Ratio: 18.0000%

// hexadecimal number
echo MyStringClass::format('{0:X}', 10);        // A
echo MyStringClass::format('{0:X3}', 10);       // 00A
```

Requirement
-----------

[](#requirement)

PHP 7.2 or later

Installing stk2k/string-format
------------------------------

[](#installing-stk2kstring-format)

The recommended way to install stk2k/string-format is through [Composer](http://getcomposer.org).

```
composer require stk2k/string-format
```

After installing, you need to require Composer's autoloader:

```
require 'vendor/autoload.php';
```

License
-------

[](#license)

This library is licensed under the MIT license.

Author
------

[](#author)

[stk2k](https://github.com/stk2k)

Disclaimer
----------

[](#disclaimer)

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

1803d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/955f3d564811dc5e1212c1a7a66bc4eb7e11ac166cc38600f244dc25f97d3ef4?d=identicon)[stk2k](/maintainers/stk2k)

---

Top Contributors

[![stk2k](https://avatars.githubusercontent.com/u/985640?v=4)](https://github.com/stk2k "stk2k (9 commits)")

---

Tags

phpformatterstringformatC sharp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stk2k-string-format/health.svg)

```
[![Health](https://phpackages.com/badges/stk2k-string-format/health.svg)](https://phpackages.com/packages/stk2k-string-format)
```

###  Alternatives

[coduo/php-to-string

Simple library that converts PHP value into strings

27112.7M10](/packages/coduo-php-to-string)[hallindavid/manny

a package of manipulators that hopefully come in useful for those of us who always forget regex when we need it (manny is short for manipulation)

38103.3k2](/packages/hallindavid-manny)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
