PatternFormatter

January 21, 2026 · View on GitHub

The PatternFormatter enables advanced pattern-based string transformation using filtering patterns and transformation directives.

Usage

Basic Filtering

use Respect\StringFormatter\PatternFormatter;

$formatter = new PatternFormatter('000-0000');

echo $formatter->format('1234567890');
// Outputs: "123-4567"

Case Transformations

use Respect\StringFormatter\PatternFormatter;

$formatter = new PatternFormatter('\\l###\\U###');

echo $formatter->format('abCDEF');
// Outputs: "abcDEF"

Repetition Quantifiers

use Respect\StringFormatter\PatternFormatter;

// Match all digits (one or more)
$formatter = new PatternFormatter('0+');

echo $formatter->format('abc123def456');
// Outputs: "123456"

// Match all characters (zero or more)
$formatter = new PatternFormatter('#*');

echo $formatter->format('hello');
// Outputs: "hello"

API

PatternFormatter::__construct

  • __construct(string $pattern)

Creates a new formatter instance with the specified pattern.

Parameters:

  • $pattern: The pattern string defining transformation rules

Throws: InvalidFormatterException when pattern is empty

format

  • format(string $input): string

Formats the input string according to the pattern rules, applying filters and transformations.

Parameters:

  • $input: The string to format

Returns: The formatted string with transformations applied

Pattern Syntax

Filtering Patterns

PatternDescription
#Any character
0Digits only (0-9)
AUppercase letters only
aLowercase letters only
CLetters (upper/lower) only
WWord characters (alphanumeric) only

Repetition Quantifiers

Filters can be followed by a repetition quantifier to match multiple characters:

PatternDescription
X+Match filter X one or more times
X*Match filter X zero or more times
X{n}Match filter X exactly n times
X{n,m}Match filter X at least n and at most m times
X{n,}Match filter X at least n times (no upper limit)
X{,m}Match filter X at most m times (zero minimum)

Where X is any filtering pattern (#, 0, A, a, C, W).

Examples:

PatternDescription
0+Digit one or more times
C*Letter zero or more times
C{3}Exactly 3 letters
A{5,10}Uppercase letter 5 to 10 times
#{,5}Any character up to 5 times

Transformation Patterns

PatternDescription
\dDelete the character
\lLowercase next character
\LLowercase until \E
\uUppercase next character
\UUppercase until \E
\iInvert case next character
\IInvert case until \E
\EEnd the transformation state

Escape Sequences

PatternDescriptionExample
\#Literal # characterMatches # literally
\0Literal 0 characterMatches 0 literally
\ALiteral A characterMatches A literally
\@Literal @ characterMatches @ literally

Literal Characters

Any character not defined as a pattern (A, a, 0, #, C, W, \) is treated as a literal and appears in the output as-is.

Behavior

Filtering Patterns

  • Remove non-matching characters: Characters that don't match the filter are skipped
  • Keep matching characters as-is: When characters match the filter, they pass through unchanged
  • Consume from input: Filters advance the input pointer when they find a match

Transformation Patterns

  • Stateful transformations: \L, \U, \I persist until reset
  • Single-character transformations: \d, \l, \u, \i affect only the next character
  • End of transformations: \E clears any active transformation state
  • Unicode aware: Transformations work with international characters

Repetition Quantifiers

  • Greedy matching: Repetitions consume as many matching characters as possible up to the maximum
  • Non-matching characters skipped: Characters that don't match the filter are skipped over
  • Works with transformations: Repetitions can be combined with case transformations
  • Partial matches allowed: If fewer characters match than the minimum, all available matches are returned

Examples

PatternInputOutputDescription
000-00001234567123-4567Phone number formatting
AAA-000ABC123ABC-123License plate format
\U###abcABCUppercase until reset
\L####ABC1abc1Lowercase until reset
\l#\u#AbaBCase transformation
\I####AbCdaBcDCase inversion until reset
CC00WWAB123DAB123DInternational postal code
(000) 000-00001234567890(123) 456-7890US phone format
000-00-0000123456789123-45-6789SSN format
\L##\E##ABCDabCDTransformation reset
##\d##ABCDEABDEDeleting character
0+a1b2c3123All digits (one or more)
C*abc123abcAll letters (zero or more)
#{,5}abcdefghabcdeUp to 5 characters
A{2,4}ABCDEFGABCD2 to 4 uppercase letters
C+-0+ABC-123ABC-123Letters then digits

International Support

The formatter works with Unicode characters and international text:

$formatter = new PatternFormatter('\\U##');

echo $formatter->format('ñáçé');
// Outputs: "Ñá"

$formatter = new PatternFormatter('CC');

echo $formatter->format('ñáç123');
// Outputs: "ñá"

Edge Cases

PatternInputOutputReason
###ababPattern longer than input uses all available
####abcdefghabcdInput longer than pattern truncates
C0ABC123A1Non-matching characters are skiped
AAA123(empty)No matching characters found
\E####abc🙂abcTransformation with no active state