Keyword Extractor

bubble

A simple little PHP function to automatically extract keywords for addition into your content.

Here is a useful little PHP function for anyone that wishes to use it.

It's a keyword extractor that searches a body of text and extracts words that occur according to a set amount of times that you specify, compares them to an array of common words to ignore and then returns a comma separated list of the words found as a string for inclusion into your page.

I use it in the top om my content pages to generate extra keywords.

Not a big deal, but can be quite useful.


// common words to ignore in meta keyword extract.
// extend as required
$stopwords = array(
"about",
"above",
"across",
"after",
"afterwards"
);
function keyword_extract($text, $minStrLen = 2, $minOccur = 1)
{
global $stopwords;
$text = strip_tags($text);
$text = str_replace("\n","", $text);
$text = str_replace("\r","", $text);
$text = str_replace(",","", $text);
$text = str_replace(".","", $text);
$text = str_replace(";","", $text);
$text = strtolower($text);
$punc =". , : ; ' ? ! ( ) \" \\";
$punc = explode(" ",$punc);
foreach($punc as $value){
$text = str_replace($value, " ", $text);
}
$words = explode(" ", $text);
$keywords = array();
foreach ($words as $value) {
$common = false;
if (strlen($value) > $minStrLen){
foreach($stopwords as $commonWord){
if ($commonWord == $value){
$common = true;
}
else{
}
}
if($common != true){
$keywords[] = $value;
}
else{
}
}
else{
}
} $keywords = array_count_values($keywords);
arsort($keywords);
$str = "";
foreach ($keywords as $key => $value) {
if ($value > $minOccur){
$str .= $key.", ";
}
}
$str = trim($str, ", ");
return $str;
}

Call it into your keywords like this:


keyword_extract($CONTENT [string], 2 [int], 1 [int]);

and presto, instant keyword list.

bubble

Posted on Monday 16th June, 2008 at 11:09 am by Paul Whitrow, and filed under and

Comments about ‘Keyword Extractor’

pwhitrow
3rd December 2008, 7:36 am
bubble

Hi Ash,

You would call it like this:

< meta name="keywords" content="< ?php echo keyword_extract($YOURTEXT)?>" />

$YOURTEXT is the text (content) that you want to extract the keywords from.

Hope that helps. Give me a call if not.

Ash Thornton
2nd December 2008, 6:33 pm
bubble

I'm stuck on how to use this :) I need some heeeellllppp!! Please :)

I've put the script into a seperate php file and included it at the top of my header.php then called it in like you said... When you say to call it into your keywords, do you mean the meta tag of keywords or something else? because that's what I'm doing. It's probably stupidly obvious but I can't figure it out :)