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
mybook

http://www.packtpub.com/php-and-script-aculo-us-web-2-0-application-interface/book

19 comments so far

  1. The Cruisemaniac on

    Good work shri!

    Nice hack…

  2. srinix on

    Thanx Ashwin

  3. Prem & Chandru on

    sdkfjadkfjka

  4. Prem & Chandru f on

    hi goog

  5. kate on
  6. mike on

    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

  7. Edwin on

    Nice tutorial, thx Shri!

  8. Edward Edmonds on

    Great function, any idea how to do the above word count but leave in say tags.

  9. wahyu on

    thank you for the source.
    your code really usefull for me.
    once again thank’s

    wahyu.

  10. swiss on

    mate, what about http://www.php.net/str_word_count

  11. pufone on

    Yeah, str_word_count should do it, no need for hacks m8.

  12. Beyoncé on Gotham magazine on

    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 ?

  13. lawynya on

    Hello!! Could someone help me with a php function that counts the syllables of a word??

  14. Catur on

    Thanks for the nice tutorial. I Get what i want now.

  15. Strong Man on

    what a long article. I tired when I read this post. But your article had been effective. Thanks so much for this post.

  16. RaiulBaztepo on

    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

  17. PiterKokoniz on

    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.

  18. Justin on

    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

  19. yasir on

    how to use this script where to put this code please help me


Leave a reply