Skip to content

Core\Str::indexOf

Core\Str::indexOf(
    string $haystack,
    string $needle,
    {from?: int, caseInsensitive?: bool},
): ?uint

Finds where a piece of text first appears in a string.

Core\Str::indexOf searches $haystack for $needle and returns the position of the first match. The first character is position 0. When the needle is not in the string, the result is null. Searching "The quick brown fox" for "quick" gives 4.

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. from starts the search at a later position. A negative from counts back from the end of the string. caseInsensitive: true treats upper-case and lower-case letters as the same.

Good to know: to check only whether the text is in the string, use Core\Str::contains. To get the text before or after a match, use Core\Str::before or Core\Str::after.

Parameters

  • $haystackstringThe string to search in.
  • $needlestringThe string to search for. An empty needle matches where the search starts.

Options

  • fromintoptionalThe position where the search starts. A negative value counts from the end. The default is 0.
  • caseInsensitivebooloptionalWhen true, upper-case and lower-case letters match each other. Each character is compared by its lower-case form, so ß does not match SS. The default is false.

Return value

Returns ?uint. The position of the first match at or after from, counted in characters as Core\Str::length counts them. You can pass it to Core\Str::slice. The result is null when there is no match.

Examples

Find a needle

<?nvs
// Shows how to find a word in a text, and how to check that a word is not there.
var $s = 'The quick brown fox';
// "quick" starts at position 4, so this prints `found at 4`.
var $at = Core\Str::indexOf($s, 'quick');
if ($at != null) {
echo 'found at ', $at, "\n";
}
// "wolf" is not in the text, so the result is `null` and this prints `no wolf here`.
if (Core\Str::indexOf($s, 'wolf') == null) {
echo "no wolf here\n";
}
Output
found at 4
no wolf here

Options

<?nvs
// Shows the two options: where the search starts, and whether upper and lower case must match.
var $s = 'one, two, one, two';
// `from: 5` starts the search at position 5, so this prints the second "one" at `10`.
echo Core\Str::indexOf($s, 'one', { from: 5 }) ?? 'none', "\n";
// A negative `from` counts from the end. This searches the last 3 characters and prints `15`.
echo Core\Str::indexOf($s, 'two', { from: -3 }) ?? 'none', "\n";
// `caseInsensitive: true` finds "two" when you search for "TWO", so this prints `5`.
echo Core\Str::indexOf($s, 'TWO', { caseInsensitive: true }) ?? 'none', "\n";
Output
10
15
5

Mark a search term

<?nvs
// Shows how to mark the search term in a product name, as a shop does in its search results.
class Search
{
// This puts `[` and `]` around the first match. If there is no match, the name stays the same.
public static function mark(string $name, string $term): string
{
var $at = Core\Str::indexOf($name, $term, { caseInsensitive: true });
if ($at == null) {
return $name;
}
uint $end = $at + Core\Str::length($term);
return Core\Str::slice($name, 0, $at as int)
. '['
. Core\Str::slice($name, $at as int, Core\Str::length($term) as int)
. ']'
. Core\Str::slice($name, $end as int);
}
}
// The marked part keeps the case of the name. This prints `Crème [Brûlée] set`,
// `[brûlée] torch` and `Coffee cup`.
array<string> $products = ['Crème Brûlée set', 'brûlée torch', 'Coffee cup'];
foreach ($products as string $product) {
echo Search::mark($product, 'BRÛLÉE'), "\n";
}
Output
Crème [Brûlée] set
[brûlée] torch
Coffee cup