alain mardo alain mardo - 4 months ago 11
PHP Question

Top 10 keywords PHP in a string

I made a complex array of keywords when my goal is to present the top 10 words are in the string.

b) I just want to introduce a words of importance rather than words like "The,That,to,a...".



The Full Code:

$str= $db_tag;
$tok = strtok($str, ", ");
$subStrStart = 0;

while ($tok !== false) {
preg_match_all("/\b" . preg_quote($tok, "/") . "\b/", substr($str, $subStrStart), $m);
if(count($m[0]) >= 10)
echo "'" . $tok . "' found more than 10 times, exaclty: " . count($m[0]) . "<br>";
$subStrStart += strlen($tok);
$tok = strtok(", ");
}


My string:

$db_tag="The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";


Thanks in advance.

Answer

Try this:

$db_tag = "The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";

$words = explode(',', $db_tag);
$counts = array_count_values($words);
asort($counts);
$topTen = array_reverse(array_slice($counts, -10, null, true));

var_dump($topTen);

You should see:

php > var_dump($topTen);
array(10) {
  ["England"]=>
  int(5)
  ["Bank"]=>
  int(5)
  ["Brexit"]=>
  int(5)
  ["Economy"]=>
  int(4)
  ["Vote"]=>
  int(4)
  ["The"]=>
  int(2)
  ["Post"]=>
  int(1)
  ["Given"]=>
  int(1)
  ["A"]=>
  int(1)
  ["Could"]=>
  int(1)
}

First, we split the string into an array with explode(). Then, we return an array of unique array values with array_count_values(), associated with the count of their occurrence in the string. Next, we sort the array in-place by value using asort(). Then, we slice off the last 10 elements from the array (the highest ones) with array_slice() and then reverse it with array_reverse() to put them in descending order (optional).

Comments