-2

I have a table in database

|ID| Sentence        |  
|1 | I have a Rabbit |     
|2 | I have a Turtle |

How to count every word in that table (or this is a TF-IDF Raw method)?

I  = 2 
have = 2
a = 2 
Rabbit = 1 
Turtle = 1 

Anybody help me please with PHP code?

  • I think the count for `I` is 2, not 3 – Nick Dec 24 '18 at 04:11
  • Take a look at [`explode`](http://php.net/manual/en/function.explode.php) and [`array_count_values`](http://php.net/manual/en/function.array-count-values.php) – Nick Dec 24 '18 at 04:12

1 Answers1

1

You need to concat all string to a variable and then do like this

$words = 'Word count count';
print_r( array_count_values(str_word_count($words, 1)) );

http://sandbox.onlinephpfunctions.com/code/e7820275a94fbcf6fa814d4886167e201ce03de3

Shibon
  • 1,552
  • 2
  • 9
  • 20