dogdogdogdogsdogdogdogs
how would I count how many times "dog" and "dogs" appeared without regex?
Use substr_count()
substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.
However, you say you want to count the occurrences of dog
and dogs
. If you check for dogs
first and then for dog
, you will get skewed results (because dogs
gets counted twice).
If your example is literally dog
and dogs
, you need to subtract the count for dogs
from that for dog
to get the proper count.
If you are working on a programmatic approach with varying words, you will need to check beforehand whether any of the words are a part of another word.
Cheers to SilentGhost for the simpler approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With