How to Calculate Keyword Density of a Web Page

Posted on 2003-11-10
Medium Priority
Last Modified: 2012-06-21
hi!!! guys,

I got a pblm in calcualting Keyword Density.
First thing is I want to know is for which terms Keyword density is calculated.
Whether to take the terms from Title Tag or from different means.

What is the Formula for calculating the same.
i guess this is the formula
Keyword Density is the ratio of a keyword or keyphrase to the total words (depth) on a page.

waiting for the suggestions.....

Question by:kiransnaik
1 Comment
LVL 13

Accepted Solution

lozloz earned 200 total points
ID: 9718143

firstly, if you're loading in an html file, i suppose the best thing to do would be to strip the tags out so you're just left with content, which should be the title and the body.. this should work unless there's some javascript which might be left in, you'll need to test that though. you can use fopen and fread to read in the file into a variable then use $words_to_count = strip_tags($words_to_count);. actually i found this function in the manual which i changed slightly:

$words_to_count = strip_tags($body);
$pattern = "/[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/";
$words_to_count = preg_replace ($pattern, " ", $words_to_count);
$words_to_count = trim($words_to_count);
$words = explode(" ",$words_to_count);
$total_words = count($words);

then you have the words in $words and the number in $total_words. so you just loop through checking for equivalance to your keywords (if they're stored as an array in $keywords):

$numkeys = 0;
for($x = 0; $x < $total_words; $x++) {
  foreach($keywords as $key => $value) {
    if($keywords[$key] == $words[$x]) {
$density = $numkeys / $total_words; # fraction of keywords in all words
$density = $density * 100; # percentage of words in string that are any of the keywords

tell me how you get on


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question