Skip to content

Core\Str::countOf

Counts how many times a piece of text appears in a string.

Core\Str::countOf returns a whole number. When the text does not appear at all, the result is 0. Each match starts after the end of the previous one, so matches never overlap. In "aaa", the text "aa" is counted once, and the result is 1.

The search is case-sensitive, so "error" does not count "Error". To ignore upper and lower case, change both strings to lower case with Core\Str::lower first. The text to count must not be empty. An empty text throws a RuntimeError.

To check only whether the text appears, use Core\Str::contains.

The examples below count the commas in a line, show that matches do not overlap, and check that each placeholder appears exactly once in an email template.

Parameters

  • $haystackstringThe string to search in.
  • $needlestringThe text to count. The search is case-sensitive. It must not be empty.

Return value

Returns uint. The number of times $needle appears, or 0 when it does not appear. Matches do not overlap, so countOf("aaa", "aa") is 1.

Errors

  • RuntimeError$needle is empty.

Examples

Count the commas

<?nvs
// Shows how to count the separators in a line to find out how many fields it has.
string $line = '1042,Ada,Lovelace,London';
// The line has 3 commas. This prints `3`.
uint $commas = Core\Str::countOf($line, ',');
echo $commas, "\n";
// A line has one field more than it has commas. This prints `4 fields`.
echo $commas + 1, " fields\n";
// When the text does not appear, the result is `0`. This prints `0`.
echo Core\Str::countOf($line, ';'), "\n";
Output
3
4 fields
0

Matches do not overlap

<?nvs
// Shows the three rules of the count: no overlap, exact case, and no empty text.
// Each match starts after the end of the previous one.
// "aa" fits once into "aaa" and twice into "aaaa". This prints `1` and then `2`.
echo Core\Str::countOf('aaa', 'aa'), "\n";
echo Core\Str::countOf('aaaa', 'aa'), "\n";
// The search is case-sensitive. Only the last word matches. This prints `1`.
echo Core\Str::countOf('Error ERROR error', 'error'), "\n";
// An empty text to count throws a `RuntimeError`. This prints the error message.
try {
echo Core\Str::countOf('abc', ''), "\n";
} catch (RuntimeError $error) {
echo 'cannot count: ', $error->message, "\n";
}
Output
1
2
1
cannot count: Core\Str::countOf(): the needle must not be empty

Check an email template

<?nvs
// Shows how to check that an email template uses each placeholder exactly once.
string $template = "Hello {name},\n"
. "your order {order} has shipped.\n"
. "Thank you, {name}!\n";
array<string> $placeholders = ['{name}', '{order}', '{date}'];
// `{name}` appears twice, `{order}` once and `{date}` never.
// This prints `used 2 times`, `ok` and `missing`.
foreach ($placeholders as string $placeholder) {
uint $found = Core\Str::countOf($template, $placeholder);
if ($found == 0) {
echo $placeholder, ": missing\n";
} elseif ($found == 1) {
echo $placeholder, ": ok\n";
} else {
echo $placeholder, ': used ', $found, " times\n";
}
}
Output
{name}: used 2 times
{order}: ok
{date}: missing