Automatic SEO Keyword Generation from Dynamic Content
I wrote this over a year ago now – it was my first attempt at automatic meta compilation based on dynamic page content. It was geared towards sites that needed some basic SEO, but didnt want to hire a SEO professional. It does a pretty good job, but it can definitely be improved upon and I plan to do just that sometime soon (when I have time, smile)
But thought I’d put what I wrote “out there” for anyone interested in it. You’d call it by initializing the class just after the <HTML> tag, feed it any forced keyword priority (if so desired), and call $yourobj->GenSEO at the end of the page (after </HTML>). The class will read your content, determine popular keywoards (with priority towards <H#> tags) and generate meta keyword, title, and description.
class SEO {
private $kwarray = array();
private $words = array();
private $common = array();
private $top = array();
public $desc="META KEYWORD SEO v1.0";
public $title="PROFFER WAS HERE!";
public function __construct() {
ob_start();
$this->Add("to be or not the them they was were have has had saw a go my mine you your yours our ours theirs of with without within she he his hers him her shall");
$this->Add("should could would might probably an be does being ought been did going that than also yes no maybe etc get through if any also me b c d e f g h i j k");
$this->Add("l m n o p q r s t u v w x y z as in by am do we some which here it went goes from is are and for st rd nd th one two three four five six seven eight nine");
$this->Add("ten its at what how where why this can can zero will now on put when put see non said let each way new call end use try used tried lie lied may need more");
$this->Add("want all common next just such like done using");
}
public function GenSEO($data,$max=20,$meta=FALSE) {
$specialitems=array();
for ($i=1; $i<6; $i++) {
preg_match_all("/<[Hh]{$i}[^>]*>(.*?)<\/[Hh]{$i}>/",$data,$specials,PREG_SET_ORDER);
if (count($specials)>0) {
for ($j=0; $j<count($specials); $j++) { $specialitems[$specials[$j][1]]=15; }
}
}
$this->words=$specialitems;
$_d = preg_replace("/&|"|©| |‐|<|>|=|\+|\<|\>|\r|\n|\t|\[|\]|\/|\(|\)|,|\||\.|\*|\%|\$|\d{1,99}|\'|\"|:|\?|\!|\@|\-|\<97>|;|_/i"," ",strip_tags($data));
$_d = preg_split("/\s|$/i",$_d,-1,PREG_SPLIT_NO_EMPTY);
for ($i=0; $i<count($_d); $i++) {
$word=strtolower($_d[$i]);
if (!$this->common[$word]) { $this->words[$word]++; }
}
array_multisort($this->words,SORT_DESC);
$this->top=array_slice($this->words,0,$max);
$top = array_keys($this->top);
if ($meta) {
$meta = "<meta name=\"keywords\" content=\"".implode(", ",$top)."\" />\r\n";
$desc = "<meta name=\"description\" content=\"{$this->desc}\" />\r\n";
echo "
<HEAD>
<TITLE>{$this->title}</TITLE>
$meta
$desc
</HEAD>
$data
";
} else {
echo "Top $max:<HR>".implode("<BR>",$top)."<hr>";
}
}
public function Add($e) {
$s = preg_split("/\s/i",$e,-1,PREG_SPLIT_NO_EMPTY);
foreach ($s as $e) {
$this->common[$e]=TRUE;
}
}
public function Force($word,$score) { $this->words[$word]=$score; }
};
You’re free to use this code under the terms of the GPL v3.
Popularity: 24% [?]
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Good work really thinking in the sense of like this is really beautiful which makes you different than others.