Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to generate a list of single word keywords from text in PHP

1 Answer

0 votes
function generateKeywordsFromText($text) {
    $stopWords = array(
		'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you',
		'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 
		'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 
		'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 
		'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 
		'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 
		'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 
		'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 
		'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 
		'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 
		'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 
		'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 
		'can', 'will', 'just', 'don', 'should', 'now', 'means', 'mean');

    $text = trim($text); 
    $text = preg_replace('/\s\s+/i', '', $text); // replace multiple spaces
    $text = strtolower($text); 

    preg_match_all('/\b.*?\b/i', $text, $wordsArr);
    $wordsArr = $wordsArr[0];
    foreach ($wordsArr as $key => $word) {
        if ($word == '' || in_array($word, $stopWords) || strlen($word) <= 2 ) {
              unset($wordsArr[$key]);
        }
    }   
	
    $wordOccurrences = array();
    foreach ($wordsArr as $key => $word ) {
        if (isset($wordOccurrences[$word]) ) {
            $wordOccurrences[$word]++;
        } else {
            $wordOccurrences[$word] = 1;
        }
    }

    $wordOccurrences = array_slice($wordOccurrences, 0, 12); // take only 12 keywords
	  
	$keywords = "";
	foreach ($wordOccurrences as $key=>$value)
	       $keywords .= ", " . $key;
	  
    return trim($keywords, ",");
}

echo generateKeywordsFromText("PHP is a general-purpose scripting language especially suited " .  
                              "to web development. It was originally created by Danish a " .
                              "Canadian programmer Rasmus Lerdorf in 1994. The PHP reference " .
                              "implementation is now produced by The PHP Group");




/*
run:

php, general, purpose, scripting, language, especially, suited, web, 
development, originally, created, danish

*/

 



answered Jun 6, 2021 by avibootz
edited Jun 6, 2021 by avibootz

Related questions

1 answer 175 views
1 answer 192 views
1 answer 174 views
1 answer 291 views
1 answer 199 views
1 answer 197 views
...