How To Count How many word repeated in the string line
Hello Friends Thank You For Trying Help
How many times word found in the string line
for eample I need to make sure word
$text = "Hello friends how are you friends";
if($friends > 1){ // if friends found and more than 1 time
// Word Found more than 1 time
}
else {
// Word not found or only found 1 time
}
if ($result['friends'] > 1) { // if friends found and more than 1 time
// Word Found more than 1 time
} else {
// Word not found or only found 1 time
}
Thanks For answer it works now -- i will mark your answer now -- but please when i use key not found in string give me undefind index try make it return 0 .. to be useful for others THANKS SO MUCH FOR YOU EFFORT HONOR FOR ME TO TALK WITH YOU
function getWordCounts($string,$word)
{
$resultArray = array_count_values(str_word_count($string, 1));
if (array_key_exists($word, $resultArray)) {
return $resultArray[$word];
}
return 0;
}
// usage
$string = "Hello friends how are you friends";
getWordCount($string,'friends');
// return count if found
// return 0 if not found