Core\Str::lastIndexOf
Core\Str::lastIndexOf(
string $haystack,
string $needle,
{before?: int, caseInsensitive?: bool},
): ?uintFinds where a piece of text appears for the last time in a string.
Core\Str::lastIndexOf searches $haystack for $needle and returns the position of the last match. The first character is position 0. When the needle is not in the string, the result is null. Matches may overlap, so searching "banana" for "ana" gives 3.
The position counts characters the way a person sees them, so an accented letter or an emoji counts as one. You can give the position to Core\Str::slice to cut the text at the match.
Two options change the search. before searches only the characters in front of that position, so a match must end there or earlier. A negative before counts back from the end of the string. caseInsensitive: true treats upper-case and lower-case letters as the same.
The examples below find the last match in a file path, use both options, and shorten a long text at the last space.
Parameters
$haystackstringThe string to search in.$needlestringThe string to search for.
Options
beforeintoptionalSearches only the characters in front of this position, so a match must end here or earlier. A negative position counts back from the end. The default is the whole string.caseInsensitivebooloptionalWhentrue, upper-case and lower-case letters count as the same. The default isfalse.
Return value
Returns ?uint. The position of the last match, or null when there is none. Matches may overlap, so lastIndexOf("aaa", "aa") returns 1.
Examples
The last slash
<?nvs// Shows how to find where a piece of text appears for the last time in a string.
var $path = 'docs/guide/intro.md';
// The last "/" is at position 10, so this prints `last slash at 10`.var $at = Core\Str::lastIndexOf($path, '/');if ($at != null) { echo 'last slash at ', $at, "\n";}
// Matches may overlap. "ana" is in "banana" at 1 and at 3, so this prints `3`.echo Core\Str::lastIndexOf('banana', 'ana') ?? 0, "\n";
// There is no "\" in the path, so the result is `null` and this prints `no backslash`.if (Core\Str::lastIndexOf($path, "\\") == null) { echo "no backslash\n";}last slash at 103no backslashOptions
<?nvs// Shows the two options: `before` ends the search early, and `caseInsensitive` ignores case.
var $text = 'one-two-three-four';
// Without options, the last "-" is at position 13. This prints `13`.echo Core\Str::lastIndexOf($text, '-') ?? 0, "\n";
// `before: 10` searches only the first 10 characters, "one-two-th". This prints `7`.echo Core\Str::lastIndexOf($text, '-', { before: 10 }) ?? 0, "\n";
// A negative `before` counts back from the end. -5 is the same as 13 here, so this prints `7`.echo Core\Str::lastIndexOf($text, '-', { before: -5 }) ?? 0, "\n";
// With case, only the lower-case "tea" at 5 matches. This prints `5`.// With `caseInsensitive: true`, "TEA" at 10 matches too. This prints `10`.var $drinks = 'Tea, tea, TEA';echo Core\Str::lastIndexOf($drinks, 'tea') ?? 0, "\n";echo Core\Str::lastIndexOf($drinks, 'tea', { caseInsensitive: true }) ?? 0, "\n";1377510Shorten at a space
<?nvs// Shows how to shorten a long product text for a preview without cutting a word in half.
class Preview{ // `before: $max` searches only the first `$max` characters. // The text is cut at the last space there, and "..." is added. public static function short(string $text, uint $max): string { if (Core\Str::length($text) <= $max) { return $text; } var $at = Core\Str::lastIndexOf($text, ' ', { before: $max as int }); if ($at == null) { return Core\Str::slice($text, 0, $max as int) . '...'; } return Core\Str::slice($text, 0, $at as int) . '...'; }}
// This prints `Fresh roasted coffee beans...`, `Green tea` and `Extraordinarilylongproductname...`.echo Preview::short('Fresh roasted coffee beans from a small farm', 30), "\n";echo Preview::short('Green tea', 30), "\n";echo Preview::short('Extraordinarilylongproductnamewithoutspaces', 30), "\n";Fresh roasted coffee beans...Green teaExtraordinarilylongproductname...