Respect's avatar

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 
}
0 likes
10 replies
automica's avatar
automica
Best Answer
Level 54

how about:

function getWordCounts(string $phrase): array
{
    $counts = [];

    $words = explode(' ', $phrase);
    foreach ($words as $word) {
        $word = preg_replace("#[^a-zA-Z\-]#", "", $word);

        if (!$word) {
            continue;
        }
        if (array_key_exists($word, $counts)) {
            $counts[$word] += 1;
        } else {
            $counts[$word] = 1;
        }
    }

    return $counts;
}

$string = "Hello friends how are you  friends";

$result = getWordCounts($string);

var_dump($result);

returns

array(6) {
  ["Hello"]=>
  int(1)
  ["friends"]=>
  int(2)
  ["how"]=>
  int(1)
  ["are"]=>
  int(1)
  ["you"]=>
  int(1)
}

use as below:

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 
}
1 like
Respect's avatar

SORRY IT DOESN'T WORK got this error Undefined index: Hello when i run it

automica's avatar

@respect

was missing

        if(!$counts[$word]) {
            $counts[$word] = 0;
        }

try this:

function get_word_counts(string $phrase): array
{
    $counts = [];

    $words = explode(' ', $phrase);
    foreach ($words as $word) {
        $word = preg_replace("#[^a-zA-Z\-]#", "", $word);

        if(!$counts[$word]) {
            $counts[$word] = 0;
        }

        if ($word) {
            $counts[$word] += 1;
        }
    }

    return $counts;
} 
1 like
Respect's avatar

Okey Put Your Other Code and i will mark your answer appreciate your effort to help and thanks put your answer

Respect's avatar

Hello Steel got same error undefind index i recomend you have to use arry_key_exists() to check the index before return the value Try update it again

1 like
Respect's avatar

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

automica's avatar

thanks.

I've updated again, doing a initial check for $word. This means if we have a space, it doesn't register it as word.

thanks for Best Answer. Happy to help!

Respect's avatar

I found solution here https://stackoverflow.com/questions/4670417/count-word-frequency-in-a-text i updated it to prevent undefind index when it not found and put it inside the function code like this

   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

Please or to participate in this conversation.