What are some best practices for efficiently counting occurrences of a string in PHP?

When counting occurrences of a string in PHP, one efficient way is to use the `substr_count()` function, which counts the number of times a substring appears in a string. This function is case-sensitive, so make sure to account for that when counting occurrences. Another approach is to use regular expressions with `preg_match_all()` to count occurrences of a string pattern in a larger string.

// Using substr_count() to efficiently count occurrences of a string
$string = "apple orange banana apple grape apple";
$substring = "apple";
$count = substr_count($string, $substring);
echo "The string '$substring' appears $count times in the string.";

// Using preg_match_all() with regular expressions to count occurrences
$string = "apple orange banana apple grape apple";
$pattern = "/apple/";
preg_match_all($pattern, $string, $matches);
$count = count($matches[0]);
echo "The string 'apple' appears $count times in the string.";