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

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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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