3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generateKeywordsFromText($text){ // List of words NOT to be included in keywords $stopWords = array('à','à demi','à peine','à peu près','absolument','actuellement','ainsi','alors','apparemment','approximativement','après','après-demain','assez','assurément','au','aucun','aucunement','aucuns','aujourd\'hui','auparavant','aussi','aussitôt','autant','autre','autrefois','autrement','avant','avant-hier','avec','avoir','beaucoup','bien','bientôt','bon','c\'','ça','car','carrément','ce','cela','cependant','certainement','certes','ces','ceux','chaque','ci','comme','comment','complètement','d\'','d\'abord','dans','davantage','de','début','dedans','dehors','déjà','demain','depuis','derechef','des','désormais','deux','devrait','diablement','divinement','doit','donc','dorénavant','dos','droite','drôlement','du','elle','elles','en','en vérité','encore','enfin','ensuite','entièrement','entre-temps','environ','essai','est','et','étaient','état','été','étions','être','eu','extrêmement','fait','faites','fois','font','force','grandement','guère','habituellement','haut','hier','hors','ici','il','ils','infiniment','insuffisamment','jadis','jamais','je','joliment','ka','la','là','le','les','leur','leurs','lol','longtemps','lors','ma','maintenant','mais','mdr','même','mes','moins','mon','mot','naguère','ne','ni','nommés','non','notre','nous','nouveaux','nullement','ou','où','oui','par','parce','parfois','parole','pas','pas mal','passablement','personne','personnes','peu','peut','peut-être','pièce','plupart','plus','plutôt','point','pour','pourquoi','précisément','premièrement','presque','probablement','prou','puis','quand','quasi','quasiment','que','quel','quelle','quelles','quelque','quelquefois','quels','qui','quoi','quotidiennement','rien','rudement','s\'','sa','sans','sans doute','ses','seulement','si','sien','sitôt','soit','son','sont','soudain','sous','souvent','soyez','subitement','suffisamment','sur','t\'','ta','tandis','tant','tantôt','tard','tellement','tellement','tels','terriblement','tes','ton','tôt','totalement','toujours','tous','tout','tout à fait','toutefois','très','trop','tu','un','une','valeur','vers','voie','voient','volontiers','vont','votre','vous','vraiment','vraisemblablement'); //Let us do some basic clean up! on the text before getting to real keyword generation work $text = preg_replace('/\s{2,}/ui', '', $text); // replace multiple spaces etc. in the text $text = trim($text); // trim any extra spaces at start or end of the text $text = preg_replace('/[^\p{L}0-9 .-]+/u', '', $text); // only take alphanumerical characters, but keep the spaces and dashes too… $text = strtolower($text); // Make the text lowercase so that output is in lowercase and whole operation is case in sensitive. // Find all words preg_match_all('/\w+/iu', $text, $allTheWords); $allTheWords = $allTheWords[0]; //Now loop through the whole list and remove smaller or empty words foreach ( $allTheWords as $key=>$item ) { if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) { unset($allTheWords[$key]); } } // Create array that will later have its index as keyword and value as keyword count. $wordCountArr = array(); // Now populate this array with keywrds and the occurance count if ( is_array($allTheWords) ) { foreach ( $allTheWords as $key => $val ) { $val = strtolower($val); if ( isset($wordCountArr[$val]) ) { $wordCountArr[$val]++; } else { $wordCountArr[$val] = 1; } } } // Sort array by the number of repetitions arsort($wordCountArr); //Keep first 10 keywords, throw other keywords $wordCountArr = array_slice($wordCountArr, 0, 10); // Now generate comma separated list from the array $words=""; foreach ($wordCountArr as $key=>$value) $words .= " " . $key ; // Trim list of comma separated keyword list and return the list return trim($words," "); } echo $contentkeywords = generateKeywordsFromText("Hello Héllo");

preferences:
27.37 ms | 411 KiB | 5 Q