The PlaceholderFormatter replaces {{placeholder}} markers in strings with values from a parameters array.
use Respect\StringFormatter\PlaceholderFormatter;
$formatter = new PlaceholderFormatter(['name' => 'John', 'age' => 30]);
echo $formatter->format('Hello {{name}}, you are {{age}} years old.');
// Outputs: Hello John, you are 30 years old.The formatUsing method allows passing additional parameters at format time. Constructor parameters take precedence and won't be overwritten.
$formatter = new PlaceholderFormatter(['siteName' => 'MyApp', 'year' => 2024]);
echo $formatter->formatUsing(
'Welcome to {{siteName}} - Hello {{userName}}! © {{year}}',
['userName' => 'John', 'year' => 2020] // year won't override constructor value
);
// Outputs: Welcome to MyApp - Hello John! © 2024Placeholders can include modifiers that transform values. See the Modifiers documentation for details.
$formatter = new PlaceholderFormatter(['name' => 'John']);
echo $formatter->format('Hello {{name|upper}}!');
// Outputs: Hello JOHN!Creates a new formatter instance.
$parameters: Associative array of placeholder names to values$modifier: Optional modifier chain. Ifnull, uses default modifiers.
Formats the template string by replacing placeholders with parameter values.
Formats with additional parameters merged with constructor parameters. Constructor parameters take precedence.
Placeholders follow the format {{name}} where name is a valid parameter key. Modifiers can be added after a pipe: {{name|modifier}}.
Rules:
- Names must match
\w+(letters, digits, underscore) - Names are case-sensitive
- No whitespace inside braces or around the pipe
Valid: {{name}}, {{user_id}}, {{name|raw}}
Invalid: {name}, {{ name }}, {{first-name}}, {{}}
- Missing parameters: Placeholders are kept unchanged
- Null values: Converted to
`null`string representation - Empty strings: Valid replacements (placeholder becomes empty)
- Repeated placeholders: Each occurrence replaced independently
- Unicode: Fully supported in templates and values
| Parameters | Template | Output |
|---|---|---|
['name' => 'John'] |
"Hello {{name}}!" |
Hello John! |
['x' => 1, 'y' => 2] |
"{{x}} + {{y}}" |
1 + 2 |
['name' => 'John'] |
"{{name}} is {{age}}" |
John is {{age}} |
[] |
"Hello {{name}}" |
Hello {{name}} |
['active' => true] |
"Active: {{active}}" |
Active: `true` |
- No nested placeholders:
{{outer{{inner}}}} - No expressions:
{{x + y}} - No conditional logic
- No default values syntax