fmt is an open-source formatting library for PHP. It can be used as a safe and fast alternative to (s)printf. It can format custom and built-in types like DateTime along with 3rd parties like Uuid from Ben Ramsey.
If you are new to this library, see the Get started with functions section for a quick overview.
-
Simple format API with positional and named arguments
-
Format string syntax similar to Python’s format, CS String.format and C++ {fmt} format
-
Extensibility: support for user-defined types
-
Ease of use: small self-contained code base, no external dependencies, permissive MIT license
-
Locale-independence by default
See the documentation for more details.
Simple example printing arguments by positional placeholders in format specification:
use function fmt/string_format; $answer = string_format("The answer is {}.", 42); // "The answer is 42." $feeling = string_format("I'd rather be {1} than {0}.", 'right', 'happy'); // "I'd rather be happy than right."
Example showing string formatting by named arguments and named placeholders in format specification and DateTime formatting:
use function fmt/string_format; $message = string_format( 'On {when:Y-m-d} is {temp:0.1f}°C and is {cond}', temp: 32, cond: 'sunny', when: new DateTimeImmutable('now'), ); var_dump($message); // string(37) "On 2020年08月20日 is 32.0°C and is sunny."
The fmt library is maintained by Michał Marcin Brzuchalski (brzuchal).
Thanks for contributing to fmt! Just follow these single guidelines:
-
You must use feature / topic branches to ease the merge of contributions.
-
Coding standard compliance must be ensured before committing or opening pull requests by running
composer assert:cs-fixorcomposer assert:cs-lintin the root directory of this repository. -
After adding new features add documentation in
docs/folder and regenerate the documentation by runningmake docs-build. -
After adding new non release relevant artifacts you must ensure they are export ignored in the
.gitattributesfile.