Advanced Word Count for PHP!!!!!
If you have tried counting words in a string in PHP, you sure must have used “explode” function.
Its a pretty simple function and can be used in N-ways. A general simple function can be written as
$String = "This is my blog";
$words = explode(" ",String);
$num = count($words);
Now, this should give you the number of words (4) and following with the count of number of words{This, is, my, blog}.
BUT, the real problem is if your string is “Hello World”, the number of words here are 7 and count is also 7. How come? THats coz, of the fact the function explode takes delimiter as a space and will count the spaces, commas, etc also as words.
I have found a better way of doing the same. So here I am sharing with you.
Instead use this function, in which we check wheather the word is having any integer, charcter etc in it or not. If its a space we can royally ignore it
function wordCount($string){
$words = "";
$string = eregi_replace(" +", " ", $string);
$array = explode(" ", $string);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
$words[$i] = $array[$i];
}
return $words;
}
In this way, we can get the words as the user must have typed omitting the spaces and junk characters.
This function will return you a array of words in the string, and further we can even make it only Words++ to count how many words are there. So this function can actually work for 2 purposes.
1. Count the number of Words in a string
2. Get the words in array
Hope you find this useful.
CHeerz
Shri
If you are reading article, you might also be interested in the PHP and Scriptaculous Book I have authored.
The book gives you insights about effects, drag-n-drop, slideshows, applications, auto-completion, in-place editing and more. Complete code snippets and explanations.
CHECK out and BUY the PHP and Script.aculo.us book at Packt official site

http://www.packtpub.com/php-and-script-aculo-us-web-2-0-application-interface/book
19 comments so far
Leave a reply

Good work shri!
Nice hack…
Thanx Ashwin
sdkfjadkfjka
hi goog
nice one
http://www.w3answers.com
i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know i dont know
Nice tutorial, thx Shri!
Great function, any idea how to do the above word count but leave in say tags.
thank you for the source.
your code really usefull for me.
once again thank’s
wahyu.
mate, what about http://www.php.net/str_word_count …
Yeah, str_word_count should do it, no need for hacks m8.
I am looking for a function to count all my keywords for my post.
Eg. post 1 = keywords a, b and c
post 2 = keywords b
post 3 = keyeords b and c
I want a function that count the keywords
b = 3
c = 2
a = 1
Do you think I can modify yours ?
Hello!! Could someone help me with a php function that counts the syllables of a word??
Thanks for the nice tutorial. I Get what i want now.
what a long article. I tired when I read this post. But your article had been effective. Thanks so much for this post.
Hello!
Very Interesting post! Thank you for such interesting resource!
PS: Sorry for my bad english, I’v just started to learn this language
See you!
Your, Raiul Baztepo
Hello !!!! ^_^
I am Piter Kokoniz. Just want to tell, that I like your blog very much!
And want to ask you: what was the reasson for you to start this blog?
Sorry for my bad english:)
Tnx!
Piter.
I see everyone saying this is working but I don’t get any response with this script. I pass a variable $string = $_POST['string'] from a form to it a page where the function is called. Here is what my script looks like. No luck on word count.
<?php
$string = $_POST['string'];
# function wordCount($string){
# $words = “”;
# $string = eregi_replace(” +”, ” “, $string);
# $array = explode(” “, $string);
# for($i=0;$i
how to use this script where to put this code please help me