Core\Str::parts
Core\Str::parts(string<K> $text): array<string|K>Core\Str::parts( string<K> $text, ): array<string|K>
Splits a string with holes into its text and its holes.
Core\Str::parts takes a string with holes, such as a string<propertyPath<Order>>, and returns an array. The array starts with text and ends with text, and text and holes take turns. So a hole is always at an odd index. A text piece can be the empty string, for example when the string starts with a hole.
In a text piece, {{ is already one { and }} is already one }. Your code does not need to handle them again. The compiler checked each hole before the call, so every hole is a valid value.
Good to know: if the string is tainted, every text piece is tainted too. The holes are not tainted, because each one is a value the program declared.
The examples below show the pieces of a condition, a small query builder that turns each hole into a column name, and a message template with a fixed list of holes.
Parameters
$textstring<K>A string with holes, such asstring<propertyPath<Order>>. Each{name}in it is a hole.
Return value
Returns array<string|K>. The text and the holes in order. The array starts and ends with text, and text and holes take turns. So an even index is text and an odd index is a hole. Text can be the empty string. In text, {{ is already { and }} is already }. If $text is tainted, the text is tainted too. The holes are not.
Examples
Text and holes
<?nvs// Shows how to split a string with holes into its text and its holes.
class Order{ public int $total = 0; public string $status = '';}
class Show{ public static function pieces(string<property<Order>> $condition): void { // `Core\Str::parts` returns the text and the holes in order. // The first piece is text, and then text and holes take turns. bool $hole = false; foreach (Core\Str::parts($condition) as var $part) { echo $hole ? 'hole' : 'text', ': [', $part as string, "]\n"; $hole = !$hole; } }}
// This prints five lines. The first text is empty, because the string starts with a hole.Show::pieces("{total} > 100 AND {status} = 'paid'");text: []hole: [total]text: [ > 100 AND ]hole: [status]text: [ = 'paid']Holes to column names
<?nvs// Shows how a small query builder turns the holes of a condition into column names.
class Customer{ public string $email = '';}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Query{ public static function where(string<propertyPath<Order>> $condition): string { string $sql = ''; bool $hole = false; foreach (Core\Str::parts($condition) as var $part) { if ($hole) { // A hole is a property path. The builder writes it as a quoted column name. $sql = $sql . '"' . Core\Str::replace($part as string, '.', '_') . '"'; } else { // The text between the holes is added without a change. $sql = $sql . ($part as string); } $hole = !$hole; } return 'SELECT * FROM orders WHERE ' . $sql; }}
// This prints the query with "total" and "customer_email" as column names.echo Query::where("{total} > 100 AND {customer.email} LIKE '%@example.com'"), "\n";SELECT * FROM orders WHERE "total" > 100 AND "customer_email" LIKE '%@example.com'Fill a message template
<?nvs// Shows how to fill in a message template whose holes come from a fixed list of names.
class Message{ // The holes can only be `{name}` or `{date}`. Any other hole does not compile. public static function fill(string<"name"|"date"> $template, string $name, string $date): string { string $out = ''; bool $hole = false; foreach (Core\Str::parts($template) as var $part) { if (!$hole) { $out = $out . ($part as string); } elseif ($part as string == 'name') { $out = $out . $name; } else { $out = $out . $date; } $hole = !$hole; } return $out; }}
// `{{` and `}}` are text, and `parts` gives each of them as one brace.// This prints: Hello Ada, your order ships on 2026-11-02. Reply {STOP} to stop.echo Message::fill('Hello {name}, your order ships on {date}. Reply {{STOP}} to stop.', 'Ada', '2026-11-02'), "\n";Hello Ada, your order ships on 2026-11-02. Reply {STOP} to stop.