Count occurrences of a word in string | PHP program to count the number of occurrence of a word in the given string ; split the string by spaces ; search for pattern in string ; if match found increase count ; Driver code
< ? php function countOccurrences ( $ str , $ word ) { $ a = explode ( " ▁ " , $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ a ) ; $ i ++ ) { if ( $ word == $ a [ $ i ] ) $ count ++ ; } return $ count ; } $ str = " GeeksforGeeks ▁ A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks ▁ " ; $ word = " portal " ; echo ( countOccurrences ( $ str , $ word ) ) ; ? >