PHP Counter To Return All Stopwords & How Many Times They Were Found?

163 Views Asked by At

I can't seem to find anything that solves the following and thought i would ask for help.

I am trying to retrieve an Array of all the stopwords (including phrase matched words) within a string and also how many times each has been found. The following code is the nearest i have got to, which will return a $counter value for the total number of stopwords found (single instance only though, not multiple counts) and obviously does not list the words.

I have tried using preg_match_all and various Array outputs and all have resulted in "head scratching" errors.

Any help would be appreciated.

// test string
$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {

$counter = 0;   

foreach ($stopwords as $stopword) {

    $pattern = '/\b' . $stopword . '\b/i';              
    if (preg_match($pattern, $string)) {
        $counter++;
    }
}

return $counter;
}

// test - output counter only
echo counter_words($string, $stopwords);

With some modification, I am hoping to be able to return an array (presumably an associated array) where i can echo out something similar to:

Word/phrase found: "words are found", instances found "1"

Word/phrase found: "times", instances found "1"

etc...

Many Thanks

James

2

There are 2 best solutions below

3
On BEST ANSWER

You are only increasing the counter if there is a match, not on the number of matches. Use preg_match_all and count the number of matched results.

$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {

$counter = 0;   

foreach ($stopwords as $stopword) {
    $pattern = '/\b' . $stopword . '\b/i';              
        if (preg_match_all($pattern, $string, $matches)) {
             $counter += count($matches[0]);
        }
    }
    return $counter;
}

// test - output counter only
echo counter_words($string, $stopwords);

Demo: https://eval.in/709349

You also could implode the $stopwords with | if there will never be a special character in there, then you'd not need the foreach.

....

or for a count of each matched term (this also uses the implode approach).

$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {
    $pattern = '/\b' . implode('|', $stopwords) . '\b/i';
    preg_match_all($pattern, $string, $matches);
    return !empty($matches) ? array_count_values($matches[0]) : 'No matches found';
}

// test - output counter only
print_r(counter_words($string, $stopwords));

Demo: https://eval.in/709369

0
On

Check this out. It will return counter for all the words in single array:

$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';


$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {
    $output = array();

    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';
        preg_match_all($pattern, $string, $matches);
        $output[$stopword] = count($matches[0]);
    }
    return $output;
}

echo '<pre>';print_r(counter_words($string, $stopwords));exit;

Test here https://eval.in/709375