https://github.com/respect/stringformatter
A powerful and flexible PHP library for formatting and transforming strings.
https://github.com/respect/stringformatter
Last synced: 3 days ago
JSON representation
A powerful and flexible PHP library for formatting and transforming strings.
- Host: GitHub
- URL: https://github.com/respect/stringformatter
- Owner: Respect
- License: isc
- Created: 2026-01-15T22:44:36.000Z (28 days ago)
- Default Branch: main
- Last Pushed: 2026-02-02T02:58:46.000Z (10 days ago)
- Last Synced: 2026-02-02T12:35:29.643Z (10 days ago)
- Language: PHP
- Homepage:
- Size: 117 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Agents: AGENTS.md
Awesome Lists containing this project
README
# Respect\StringFormatter
[](https://github.com/Respect/StringFormatter/actions/workflows/continuous-integration.yml)
[](https://codecov.io/gh/Respect/StringFormatter)
[](https://packagist.org/packages/respect/string-formatter)
[](https://packagist.org/packages/respect/string-formatter)
[](https://packagist.org/packages/respect/string-formatter)
A powerful and flexible PHP library for formatting and transforming strings.
## Installation
```bash
composer require respect/string-formatter
```
## Usage
You can use individual formatters directly or chain multiple formatters together using the `FormatterBuilder`:
```php
use Respect\StringFormatter\FormatterBuilder as f;
echo f::create()
->mask('7-12')
->pattern('#### #### #### ####')
->format('1234123412341234');
// Output: 1234 12** **** 1234
```
### Using Formatters as Modifiers
The `PlaceholderFormatter` allows you to use any formatter as a modifier within templates:
```php
use Respect\StringFormatter\PlaceholderFormatter;
$formatter = new PlaceholderFormatter([
'date' => '2024-01-15',
'amount' => '1234.56',
'phone' => '1234567890',
]);
echo $formatter->format('Date: {{date|date:Y/m/d}}, Amount: ${{amount|number:2}}, Phone: {{phone|pattern:(###) ###-####}}');
// Output: Date: 2024/01/15, Amount: $1,234.56, Phone: (123) 456-7890
```
See the [PlaceholderFormatter documentation](docs/PlaceholderFormatter.md) and [FormatterModifier documentation](docs/modifiers/FormatterModifier.md) for more details.
## Formatters
| Formatter | Description |
| ---------------------------------------------------------- | ---------------------------------------------------------------- |
| [AreaFormatter](docs/AreaFormatter.md) | Metric area promotion (mm², cm², m², a, ha, km²) |
| [DateFormatter](docs/DateFormatter.md) | Date and time formatting with flexible parsing |
| [ImperialAreaFormatter](docs/ImperialAreaFormatter.md) | Imperial area promotion (in², ft², yd², ac, mi²) |
| [ImperialLengthFormatter](docs/ImperialLengthFormatter.md) | Imperial length promotion (in, ft, yd, mi) |
| [ImperialMassFormatter](docs/ImperialMassFormatter.md) | Imperial mass promotion (oz, lb, st, ton) |
| [MaskFormatter](docs/MaskFormatter.md) | Range-based string masking with Unicode support |
| [MassFormatter](docs/MassFormatter.md) | Metric mass promotion (mg, g, kg, t) |
| [MetricFormatter](docs/MetricFormatter.md) | Metric length promotion (mm, cm, m, km) |
| [NumberFormatter](docs/NumberFormatter.md) | Number formatting with thousands and decimal separators |
| [PatternFormatter](docs/PatternFormatter.md) | Pattern-based string filtering with placeholders |
| [PlaceholderFormatter](docs/PlaceholderFormatter.md) | Template interpolation with placeholder replacement |
| [TimeFormatter](docs/TimeFormatter.md) | Time promotion (mil, c, dec, y, mo, w, d, h, min, s, ms, us, ns) |
## Contributing
Please see our [Contributing Guide](CONTRIBUTING.md) for information on how to contribute to this project.
## License
This project is licensed under the ISC License - see the LICENSE file for details.