Use substr_count ()
substr_count () returns the number of times a needle substring occurs in a haystack row. Please note that the needle is case sensitive.
However , you say you want to count the occurrences of dog and dogs . If you check dogs first and then dog , you get distorted results (because dogs are counted twice).
If your example is literally dog and dogs , you need to subtract the counter for dogs from this for dog to get the correct count.
If you work with a programming approach with different words, you will need to check in advance whether any of the words is part of another word.
Welcomes SilentGhost for a simpler approach.
source share