<?php
include ("../includes/db_functions.inc.php");

function remove_top_100_words($str){
	$words=array(' the ',' of ',' and ',' a ',' to ',' in ',' is ',' you ',' that ',' it ',' he ',' was ',' for ',' on ',' are ',' as ',' with ',' his ',' they ',' I ',' at ',' be ',' this ',' have ',' from ',' or ',' one ',' had ',' by ',' word ',' but ',' not ',' what ',' all ',' were ',' we ',' when ',' your ',' can ',' said ',' there ',' use ',' an ',' each ',' which ',' she ',' do ',' how ',' their ',' if ',' will ',' up ',' other ',' about ',' out ',' many ',' then ',' them ',' these ',' so ',' some ',' her ',' would ',' make ',' like ',' him ',' into ',' time ',' has ',' look ',' two ',' more ',' write ',' go ',' see ',' number ',' no ',' way ',' could ',' people ',' my ',' than ',' first ',' water ',' been ',' call ',' who ',' oil ',' its ',' now ',' find ',' long ',' down ',' day ',' did ',' get ',' come ',' made ',' may ',' part ',' click ');
	return str_replace($words,' ',$str);
}
function most_common_words($str,$cap=5){
   // get rid of everything except the words
   $letters=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
   $temp=str_replace($letters,'',strtolower($str));
   //$temp=str_replace(array('1','2','3','4','5','6','7','8','9','0',' '),'',$temp);
   $str=str_replace(str_split($temp),'',$str);

   // create an array of words and an associative array of word appearance
   $words=explode(' ',$str);
   if($words)
      foreach($words as $value){
         if(str_replace(' ','',$value)!='')
            $w[$value]++;
      }
   arsort($w);

   // create string of the most common words
   $c=0;
   if($w) {
      foreach($w as $key=>$value){
         if($c < $cap) {
			 //$ret.=$key.' ';
			 // if you want to create an array of words, use the next line instead of the previous
			 $ret[]=$key;
		 }
		$c++;
	  }
	}
   // comment out this line if you're returning array, otherwise it gets rid of the last space
   //$ret=substr($ret,0,-1);

   return $ret;
}

$query = yasDB_select("SELECT `title`, `description` FROM `games` WHERE `category` = 1");
$data = '';
while ($text = $query->fetch_array(MYSQLI_ASSOC)) {
	$data = $data . ' ' . $text['title'] . ' ' . $text['description'];
}
$data = remove_top_100_words($data);
$data = most_common_words($data,$cap=5);
print_r($data);
?>