Oihana PHP

fastFormat.php

Table of Contents

Functions

fastFormat()  : string
Quickly formats a string using indexed placeholders and arguments.

Functions

fastFormat()

Quickly formats a string using indexed placeholders and arguments.

fastFormat(string|null $pattern, mixed ...$args) : string

This function replaces indexed tokens like {0}, {1}, etc. in a pattern string with corresponding values from a list of arguments or an array.

  • If arguments are passed as a list: fastFormat("{0} {1}", "hello", "world")
  • If passed as a single array: fastFormat("{0} {1}", ["hello", "world"])
Parameters
$pattern : string|null

The format string containing indexed placeholders.

$args : mixed

The values to insert, either as variadic args or a single array.

Tags
example
echo fastFormat("Hello {0}", "World");
// Output: "Hello World"

echo fastFormat("Coordinates: {0}, {1}", [45.0, -73.0]);
// Output: "Coordinates: 45, -73"

echo fastFormat("User {0} has {1} points", "Alice", 1500);
// Output: "User Alice has 1500 points"

echo fastFormat("Missing: {0}, {1}", "only one");
// Output: "Missing: only one, {1}" (missing index stays unchanged)

class Person {
    public function __toString(): string {
        return "John Doe";
    }
}
echo fastFormat("Name: {0}", new Person());
// Output: "Name: John Doe"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The formatted string with placeholders replaced by values.


        
On this page

Search results