0

i'm a newbie in php, so, i was making a word counter program. I was trying to count how many instances of specific words are in a website. So, i was using Substr_count to count the words, but the issue is that it picks up words like "sunlight" as containing words like "sun".

This is my code.

 /*When the user types the word*/
 $search = $_POST["texto"]; 

 /*The website*/
 $page = $_POST["Web"];

 $web = file_get_contents($page);

 /*Count words*/
 $result = (substr_count(strip_tags(strtolower($web)), strtolower($search)));

/*Display the information*/
if($result == 0){
echo "the word " .mb_strtoupper($search). " doesn't appear";    
}else{
echo "the word " .mb_strtoupper($search). " appears $result times";
}

Any way to fix this? I tried str_word_count and preg_match_all but this displays big numbers.

Wolvy
  • 141
  • 2
  • 12

4 Answers4

0

I would use a combination of str_word_count() to get all the words and array_count_values() to count the number of times these words appear:

# Get an array with lowercase words
$array_with_words = str_word_count(strtolower('string to analyze'), 1);

# Get a count of all unique values
$array_with_words_count = array_count_values($array_with_words);

# Get the count of the word you are looking for
$your_count = $array_with_words_count[ strtolower('your_word') ];
jeroen
  • 91,079
  • 21
  • 114
  • 132
0

The str_word_cound($expression, 1) function will give you an associative array with words, then you can loop using a foreach once and construct an array with word frequencies like so:

$expr = "My test expression. <b>My</b> world.";
$words = str_word_count(strip_tags(strtolower($expr)), 1);
$groupedWords = [];
foreach ($words as $word) {
    print_r($word);
    $groupedWords[$word] ++;
}
print_r($groupedWords);

Will print:

Array
(
    [my] => 2
    [test] => 1
    [expression] => 1
    [world] => 1
)

To check out how many times a word was used:

var_dump(array_key_exists('specific_word_you_look_for', $groupedWords) ? $groupedWords['specific_word_you_look_for'] : false); 

// will output the frequency or false if not found
Dan Ionescu
  • 3,135
  • 1
  • 12
  • 17
0

This will do the trick:

/*Count words*/
$result = preg_match_all('/\b'. strtolower($search) .'\b/', strtolower($web));
ded
  • 150
  • 1
  • 11
-1

If you want to use predefined function then use str_word_count()
example:

<?php
echo str_word_count("stack gives answer");
?>

output:3

Ravi Kant
  • 1
  • 2
  • That counts the total number of words, not the number of times a specific word occurs in the string. – jeroen Mar 01 '17 at 07:39