PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

Here is a simple function which can be used to count the total number of words present in a string in php.

For this function we are going to use inbuilt functions in php like count, explode, trim.

Function is very simple, it accepts the text.
Then we are checking if there are any extra spaces before and after the string, if it is there then removing that with trim function.

Then we are using count and explode function.

Explode will divide the text into array. (for more information on how explode works visit our post Split string with php explode function and print array)

Then we are counting the total number of array elements by using count function.



Now we are actually going to use this function.



Here is a full source code to try it out.



If you have any ideas and suggestions to improve this function then comments are always welcome.

I'm reading: function to count number of words in string in phpTweet this!

0 comments:

Post a Comment