How to Calculate Keyword Density of a Web Page

Posted on 2003-11-10
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Accepted Solution

lozloz earned 50 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

615 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