PHP program to count the number of words in a string!

Hello readers, today we are going to write a custom PHP program to count the number of words in a string, Although PHP has its own function to count the number of words which is, str_word_count(), the intention of writing this custom PHP script is to teach the basic functionalities to the new PHP learners. Especially for the school / college students who wants to learn PHP basics.

Here is the script:

Just read the comments in the above program to understand each line of the script. Basically the above PHP script will remove unwanted spaces between the words and count only the words other than numbers and special characters.

You can see the demo of this above script here:




Technologist, software engineer, blogger with experience in Web development and the Media.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *