When attempting to calculate the keyword density of a single keyword in a string of content, the formula is pretty straightforward: kwd = (keyword count / total word count ) * 100
However, what should the formula be when we are looking for keyword density of a keyword phrase?
For example, how would you calculate the keyword density of the phrase “blue widgets” in the following string?
$myContent = "Blue widgets in a field
of widgets blue makes for lots of widgets, true. But
if a widget is blue, is it still a
\"blue widget\" or just a lone widget in a sea
of blue?";
Here’s my current function
function my_keyword_density($post)
{
$word_count = my_word_count($post);
$keyword_count = my_keyword_count($post);
$density = ($keyword_count / $word_count) * 100;
$density = number_format($density, 1);
return $density;
}
How can I get a count of the number of words in the keyword phrase?
you can try something like this:
If you need to customize what is considereded a “word” you can add a parameter to the str_word_count function, see the manual page. The just add error checking where needed and it should work.
About the formula, I’d use something like this:
This way you’ll handle all the multi word keyphrase as if it’s single-worded. Hope it helps