Preg_match to find the exact number of words in string

advertisements

I want to find the number of words in a string. I used the below regex to do the same.

$count_keys = preg_match_all('/\bs.c.\b/', $str);

Here $str is the string from which we need to found the number of occurance of the word s.c , But i am getting wrong result say if their is a word #ssc or #sssc they are also counted as well. Please guide me where I am doing the mistake or please give the correct regex code to do the same.


Try this:

$str = 'this is a s.c good to check s.c ';

echo $count_keys = preg_match_all('/\bs\.c/', $str);

Output:

2