Skip to content

Core\Str::format

Fills the placeholders in a template with values, and returns the text.

Core\Str::format takes a template and a list of values. Each placeholder in the template starts with % and is replaced by one value. %s writes a string, %d a whole number and %f a number with decimals. %x writes a number in hexadecimal, and %% writes one %.

A placeholder can also set a width, such as %8s, which pads the value with spaces. A precision, such as %.2f, sets the number of decimals. A width counts "é" as one character. %2$s uses the second value, so a template can use the values in another order.

When the template is written in the code, Novis checks it when the program compiles. Every placeholder needs a value, and every value must be used. A template built while the program runs throws a LogicError if it does not fit its values. The template must be trusted text, so a string from a user is not allowed.

%f always writes a . before the decimals. For a separator between thousands, use Core\Math::format.

Parameters

  • $templatestringThe template. It must be trusted text, so a tainted string is not allowed. When the template is written directly in the code, its placeholders are checked when the program compiles.
  • ...$argumentsmixedvariadicThe values for the placeholders, in order, or by position with %1$s. Every value must be used by at least one placeholder.

Return value

Returns string. The filled-in text. A width or a precision counts characters, as Core\Str::length does. %f always writes . before the decimals.

Errors

  • LogicErrorA placeholder is not valid, the template uses more arguments than were passed, an argument is not used, or a value does not fit its placeholder. For example, %d does not accept an array.
  • RuntimeErrorA width or a precision is larger than any string this process can hold. A smaller one that is over the memory limit stops the request.

Examples

Basics

<?nvs
// Shows the most common placeholders: a string, a whole number, a number with decimals and hexadecimal.
// `%s` writes a string and `%d` a whole number. This prints `the cart has 3 items`.
echo Core\Str::format("%s has %d items\n", 'the cart', 3);
// `%.2f` writes a number with two decimals. This prints `3.14`.
echo Core\Str::format("%.2f\n", 3.14159);
// `%x` writes a number in hexadecimal. This prints `ff`.
echo Core\Str::format("%x\n", 255);
Output
the cart has 3 items
3.14
ff

Values in another order

<?nvs
// Shows how a template can use its values in a different order, as a translated sentence often needs.
// `%1$s` is the first value and `%2$d` is the second. Both templates get the same values.
string $english = "%1\$s has %2\$d new messages\n";
string $german = "%2\$d neue Nachrichten für %1\$s\n";
// This prints `Ada has 4 new messages` and then `4 neue Nachrichten für Ada`.
echo Core\Str::format($english, 'Ada', 4);
echo Core\Str::format($german, 'Ada', 4);
Output
Ada has 4 new messages
4 neue Nachrichten für Ada

A receipt in columns

<?nvs
// Shows how to print a receipt with straight columns, using a width and a precision.
// `%-12s` pads the name with spaces on the right to 12 characters.
// `%8.2f` writes the price with two decimals, padded on the left to 8 characters.
array<string> $names = ['Coffee', 'Crème brûlée', 'Water'];
array<float> $prices = [3.5, 6.25, 1.0];
// This prints three lines, and every price ends in the same column.
uint $i = 0;
while ($i < 3) {
echo Core\Str::format("%-12s %8.2f\n", $names[$i as int], $prices[$i as int]);
$i = $i + 1;
}
// `%08.2f` pads with zeros instead of spaces. This prints `Total 00010.75`.
echo Core\Str::format("%-12s %08.2f\n", 'Total', 10.75);
Output
Coffee 3.50
Crème brûlée 6.25
Water 1.00
Total 00010.75