[Drupal] How to count words, characters and display it on the bottom of all articles pages in Drupal7?

| | 1 min read

Recently I ran into a problem of counting the words, sentences and characters in every articles and to display it on the bottom of all the articles pages in Drupal site. So I have checked and found out the way of doing this task.

Counting the word. The count of words in all the articles can be done by a php function called str_word_count which returns the count of the word which is present in the article. Consider that we have taken the article in a variable called $content

$words =  str_word_count($content, 0);

Counting the characters is also easy by the functionality strlenConsider that we have taken the article in a variable called $content

$characters = strlen($content);

Storing the values:

In the modulename.install file I have created a table with the help of hook_schema functionality I have created a table to store the values with every node id

So whenever a node is inserted are updated i stored the value in the database table using the functionalities hook_node_insert and hook_node_update.To show it i used hook_node_view functionality/