I'm programming a keywords density tool, but I have some problems with finding a keyword that exists of multiple words.
I used some code of the this topic on Stack Overflow, but it doesn't fully work.
When I have for example a really large text, scraped from a webpage, it doesn't find more than one keyword. For example:
If I have the text "Hello this is me. Hello this is him. Hello this is her." and my keyword is "Hello this", it doesn't count 'hello this' more than once. But there are 3 instances of 'hello this'.
The code I have:
// Count words in the text
$word_count = explode(' ', $text);
$word_count = count($word_count);
// Count matches with the keyword
$keyword_count = preg_match_all("#{$searchterm}#si", $text, $matches);
$keyword_count = count($matches);
// Calculate density
$density = $keyword_count / $word_count * 100;
How can I make my code working for this problem?