FREE PHP Scripts and Snippets

Ready to use Free PHP scripts and snippets to use in your projects.
All Free Scripts & Snippets Complete PHP Scripts

[PHP] generate keywords from text function

>> Share on Facebook & Impress your friends <<

This is a simple function written in PHP to generate keywords from any text. Just pass any free text to it and it will generate a comma separated list of keywords.

<?php

/*
PHP SCRIPT

NAME: Php generate keywords from text function

You can provide a text to this function and it will generate keywords from that. 

This makes generation of keywords automatically... so easy.

This also supports stop words i.e. the list of words that should not be in the generated keywords. Basically these are common words like is, the, I, am etc.
*/

function generateKeywordsFromText($text){
	
	
	// List of words NOT to be included in keywords
      $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www', "such", "have", "then");
   
   
   //Let us do some basic clean up! on the text before getting to real keyword generation work
   
      $text = preg_replace('/\s\s+/i', '', $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('/[^a-zA-Z0-9 -]/', '', $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('/\b.*?\b/i', $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 generateKeywordsFromText("I want a simple php function that can generate keywords from any given text. Keyword generation is very important for me. I you have any such function then please refer me to this generate keywords from text function");

?>


Request Custom Snippet

Just $5 Onwards

 

Previously Ordered Snippets

I want a PHP function that generates five four word phrases, seven two word phrases and ten one word phrases as keywords.
Price $5 Order Now

I want a keyword generation tool which creates keywords from any web page by providing its URL. It is important that it removes all the html tags and javascript etc. before doing the work on real text.
Price $5 Order Now

I want a keyword generation tool with little modification so that it includes only the keywords that occur at least X number of times in the text.
Price $5 Order Now

I want PHP function to analyze any web page and find keyword density of most repeated keywords, 2 word phrases and three word phrases.
Price $5 Order Now

PHP Script to words number of words, characters and lines (calculated by number of full stops) and paragraphs in a text.
Price $5 Order Now

Random PHP Scripts and Snippets

Generate Random Password in PHP

PHP Script to Generate Random Password. You can set the list of characters that you want to be used in...

[PHP] Time ago calculation function

It is a very nice PHP time ago function. You gave pass any date to it and it will tell...

Make URLs in plain text clickable [PHP]

This is a simple PHP function to parse text provided and automatically hyperlink the links in the text.

PHP extract multiple emails from text

PHP Script to scan a text and extract all email ids contained in the text. It outputs email addresses as...

PHP Code to list all files in a folder

This PHP Code snippet lists all files in a folder and hyperlinks them accordingly. Very clean, easy to use function....

[PHP] generate keywords from text function

This is a simple function written in PHP to generate keywords from any text. Just pass any free text to...

Create URL Slug from text string [PHP]

PHP Script to create URL from string. It retails only URL Safe characters in the strings. So, now easily create...

Extracting all numbers from a String [PHP]

This PHP function extracts all numbers from a String and prints them as a comma separated list of numbers. Ready...