Are there best practices for determining if more than half of the words in a string match a given input in PHP?

To determine if more than half of the words in a string match a given input in PHP, you can split the string into words, count the occurrences of the input word, and compare it to half of the total number of words in the string. If the count is greater than half, then more than half of the words match the input.

function moreThanHalfWordsMatch($input, $string) {
    $words = str_word_count($string, 1);
    $wordCount = count($words);
    $inputCount = array_count_values($words)[$input] ?? 0;
    
    return $inputCount > $wordCount / 2;
}

$input = "apple";
$string = "apple banana apple orange apple";
if(moreThanHalfWordsMatch($input, $string)) {
    echo "More than half of the words match the input.";
} else {
    echo "Less than half of the words match the input.";
}