Nicer string formatting for PHP, inspired by Rust, Python and others.
Requires
- laravel/pint: ^1.21
Requires (Dev)
- pestphp/pest: ^3.7
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.0
- symfony/var-dumper: ^7.2
README
f()
is an alternative to string concatenation and PHP's sprintf()
function. It takes inspiration from how Rust handles string formatting with the format!()
macro.
It doesn't currently do everything that sprintf()
does, but it does enough to be useful.
Installation
You can install the package using Composer:
composer require ryangjchandler/f
Usage
Once installed, the package will provide a global f()
function that you can start using straight away.
f('Hello, {}', 'Ryan');
If you've already got an f()
function registered in the global namespace, or despise global helper functions, you can use the RyanChandler\F\F
class instead.
use RyanChandler\F\F; F::format('Hello, {}', 'Ryan');
Placeholder Syntax
The {}
syntax you see above is known as a placeholder. This is where the arguments you pass to f()
will be injected into the string.
Placeholder | Description | Example | Result |
---|---|---|---|
{} |
References an argument based on the position of the placeholder in the format string. | f('Hello, {}. I am {}.', 'world', 'Ryan') |
Hello, world. I am Ryan. |
{2} |
References the 3rd argument (0-based indexing). | f('{}, {}, {}, {2}', 1, 2, 3) |
1, 2, 3, 3 |
{name} |
References a named argument. | f('Hello, {name}', name: "Ryan") |
Hello, Ryan |
{:b} |
Formats an integer as binary. | f('{:b}', 42) |
101010 |
{:x} |
Formats an integer as hexadecimal. | f('{:x}', 42) |
2a |
{:o} |
Formats an integer as octal. | f('{:o}', 42) |
52 |
{:>10} |
Right-justifies the argument to a width of 10 using a single space. |
f('{:>10}', 'Hello') |
Hello |
{:<10} |
Left-justifies the argument to a width of 10 using a single space. |
f('{:>10}', 'Hello') |
Hello |
{:0>10} |
Right-justifies the argument to a width of 10 using a custom character 0 . |
f('{:0>10}', '12345') |
0000012345 |
{:0<10} |
Left-justifies the argument to a width of 10 using a custom character 0 . |
f('{:0>10}', '12345') |
1234500000 |
{:>width$} |
Right-justifies the argument to a width defined by the named argument width . |
f('{:>width$}', 'Hello', width: 7) |
Hello |
Contributing
All contributions are appreciated and welcome. Please refer to the CONTRIBUTING document for more information on how to contribute.
Credits
- Ryan Chandler
- All contributors
License
This project is licensed under the MIT license. Please refer to the LICENSE document for more information.