Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3951582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:44:52+00:00 2026-05-20T01:44:52+00:00

When attempting to calculate the keyword density of a single keyword in a string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:44:53+00:00Added an answer on May 20, 2026 at 1:44 am

    you can try something like this:

    $tot_words = str_word_count($myContent);
    $keyword_count = preg_match_all("/\bblue widgets\b/msiU", $myContent, $res);
    $kwd = ($keyword_count / $tot_words) * 100;
    

    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:

        $search_words = str_word_count("blue widgets");
        $kwd = ($keyword_count / ($tot_words - (($keyword_count -1) * $search_words));
    

    This way you’ll handle all the multi word keyphrase as if it’s single-worded. Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to retrieve the total number of comments received by content posted to
I am new to C# world. I am attempting to calculate time taken by
I'm attempting to calculate the average color of an image in Scala, where average
I am attempting to use the pthread library to calculate n fibonacci numbers where
I am attempting to create a mosaic of images in Java. I calculate the
What am I missing here while attempting to calculate the percentage complete? My percentage
In OpenGL in Delphi, I am attempting to calculate how much of a scene
I'm attempting to use EWS 2010 Managed API to get the total size of
I'm attempting to write a simple python script that will calculate the squareroot of
I am attempting to calculate the viewport width, and then subtract a value from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.