String_interpolate

String_interpolate injects strings on defined positions in an existing string.

Syntax

string string_interpolate(string $input, array|object $parameters)

Inserts the $parameters on positions defined in the $input string.

Parameters

$input: The input string where the positions for the $parameters are defined.
$parameters: The parameters defined either as an array or as JSON objects that should be inserted in the $input string.

Example

{
    "ParamArr":  ["World", "Moon"]
}
{
    OutputArr: string_interpolate('Hello, {0}! Goodbye, {1}.', ParamArr)
}
{
    "OutputArr": "Hello, World! Goodbye, Moon."
}

The above example shows a simple use of the string_interpolate function where the words “World” and “Moon” gets injected into the input string. In this example an array has been used as $parameters. Here it shows that the array index of the words is inserted in the $input string where {index} has been defined.

Notes

Object example

{
    "ParamObj": {
        "HelloName": "World", 
        "GoodbyeName": "Moon"
    }
}
{
    OutputObj: string_interpolate('Hello, {HelloName}! Goodbye, {GoodbyeName}.', ParamObj)
}
{
    "OutputObj": "Hello, World! Goodbye, Moon."
}

Above example shows how to use the string_interpolate function with a JSON object as $parameter. Here it is shown that the words “World” and “Moon” gets injected on their respective positions in the $input string where the json keys has been defined like {key}.