How to make keywords from sentence using PHP

Rumman Ansari   2022-09-03   Developer   web development > php keyword maker c   448 Share

In this blog I will show you how to make keywords from sentence using PHP.

 

// functions
function php_slug($string)  
 {  
      $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($string)));  
      return $slug;  
 }
 
 
// Keyword maker
function makeKeywords($sentence)
{ 

$sentence =  strtolower($sentence);

// Remove Special Symbols from sentence
$removeSpecialSymbol = array("  ", "," ,"." ,";" ,":", "\"", "/", "'", "“","”","(",")", "!","?");
$sentence = str_replace($removeSpecialSymbol, "", $sentence );
 

// make unique array
$words = explode(' ', $sentence); 
$uniqueWords = array_unique($words); 
  

// remove set of words from sentence
$wordsToAvoid = array("its", "a", "an", "in", "of", "to", "we", "you", "be", "was", "ware", "may", "could", "should", "would", "the", "some", "shall", "show", "will", "have", "has", "been", "that", "with", "this", "that", "when", "how", "then", "than", "into", "for");

$newArray = array_diff($uniqueWords, $wordsToAvoid); 
$sentence = implode(', ',$newArray);
 

 return $sentence;
}