Laravel Formatter
String formatting rules for Laravel, inspired by validation syntax
Format PHP strings using a simple, chainable rule syntax — 24+ built-in rules including trim, replace, limit, date_format, and custom rules. Apply formatting to single values or entire datasets with one call.
Requirements
- PHP ^8.4
- Laravel ^12.0 | ^13.0
Install with Composer
composer require t-labs-co/formatterGet started in minutes
1. Install the package
composer require t-labs-co/formatter
Upgrading from v1.x? Read the Migration Guide for a step-by-step upgrade path.
No configuration file or migrations are required — the package works immediately after installation.
2. Format a dataset
Define formatting rules as an array keyed by field name, then apply them to your data:
use TLabsCo\Formatter\Formatter;
$formatterRules = [
'title' => 'trim|replace:Local Composer Dependencies,[Local Composer Dependencies]|replace:[Local Composer Dependencies],[Composer Dependencies]|limit:150',
'publish_date' => 'date_format:Y-m-d',
];
$data = [
'title' => ' How to resolve missing Local Composer Dependencies on CentOS 8? ',
'publish_date' => '2024/05/02 13:00',
];
$formatted = Formatter::make($data, $formatterRules)->format()->formatted();
/*
Output:
$formatted = [
'title' => 'How to resolve missing [Composer Dependencies] on CentOS 8?',
'publish_date' => '2024-05-02'
];
*/
3. Format a single value
use TLabsCo\Formatter\Formatter;
$result = Formatter::singleFormat(' hello world ', 'trim|upper');
// 'HELLO WORLD'
4. Create custom formatting rules
Extend FormatterRule to create your own rules:
use TLabsCo\Formatter\Rules\FormatterRule;
class Max100FormatRule extends FormatterRule
{
public function format(string $attribute, mixed $value): mixed
{
if (strlen((string) $value) > 100) {
return substr((string) $value, 0, 100);
}
return $value;
}
}
// Using with custom rule
$formatterRules = [
'title' => 'trim|limit:150',
'short_description' => ['trim', new Max100FormatRule()],
];
Features
Pipe-separated rule syntax
Define multiple formatting rules in a single string, separated by pipes. Rules are applied left-to-right, with each rule’s output becoming the next rule’s input.
24+ built-in rules
| Rule | Description | Example |
|---|---|---|
boolean |
Convert to boolean | boolean or boolean:0 for 1/0 |
integer |
Convert to integer | integer |
float |
Convert to float | float |
pricing |
Format as pricing number | pricing:2 |
trim |
Remove leading/trailing whitespace | trim or trim:.,! |
trimstart |
Trim leading whitespace | trimstart |
trimend |
Trim trailing whitespace | trimend |
replace |
Case-insensitive string replace | replace:old,new |
replacefirst |
Replace first occurrence | replacefirst:old,new |
replacelast |
Replace last occurrence | replacelast:old,new |
limit |
Truncate to N characters | limit:50 or limit:50,-- |
upper |
Convert to UPPER CASE | upper |
lower |
Convert to lower case | lower |
title |
Convert to Title Case | title |
studly |
Convert to StudlyCase | studly |
date_format |
Format date strings | date_format:Y-m-d |
timestamp |
Convert to Unix timestamp | timestamp |
md5 |
Generate MD5 hash | md5 |
pad / padleft / padright |
Pad strings | pad:10,* |
substring |
Extract substring | substring:0,5 |
url_param |
Extract URL query parameter | url_param:page |
Custom rules
Create custom rules by extending the FormatterRule abstract class. Rules can be used inline with rule arrays or registered globally.
Dataset formatting
Apply formatting rules to entire arrays or collections with a single call, ensuring consistent formatting across all values.
For complete documentation, see the README on GitHub.
Hire me for Laravel string processing and data pipeline work
Need a package tailored to your application? From recurring Laravel maintenance tasks to reusable components, I can help design, build, and review production-ready solutions.
Discuss your project
Support this package