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;  } [/sourcecode] 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 book  at Packt official site


Tags: , , ,

About srinix

I am Sridhar, a matured nut, on this planet with water, air and love ;)

28 responses to “Advanced Word Count for PHP!!!!!”

  1. The Cruisemaniac says :

    Good work shri!

    Nice hack…

  2. Prem & Chandru says :


  3. Prem & Chandru f says :

    hi goog

  4. mike says :

    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

  5. Edwin says :

    Nice tutorial, thx Shri!

  6. Edward Edmonds says :

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

  7. wahyu says :

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


  8. pufone says :

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

  9. Beyoncé on Gotham magazine says :

    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 ?

  10. lawynya says :

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

  11. Catur says :

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

  12. Strong Man says :

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

  13. RaiulBaztepo says :

    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

  14. PiterKokoniz says :

    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:)

  15. Justin says :

    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.

    $string = $_POST[‘string’];
    # function wordCount($string){
    # $words = “”;
    # $string = eregi_replace(” +”, ” “, $string);
    # $array = explode(” “, $string);
    # for($i=0;$i

  16. yasir says :

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

  17. Greg says :

    Nice tutorial

  18. online stock trading advice says :

    what a great site and informative posts, I will add a backlink and bookmark your site. Keep up the good work!

    I’m Out! 🙂

  19. datshay says :

    Thanx but it doesnt count the exact word… SOmetimes it shows more… sometimes less…

  20. Jonathan Nieto says :

    Mmmm I think str_word_count will made it. Btw look at the date of the writing.


  21. Safety Shoes Kings says :

    Thank You for Information and nice themes 🙂

  22. 140 character sms says :

    thanks for this can i count wordpress post character with this script or any other way ?

  23. Adrian says :

    My simple way to limit the number of words characters in some string and count it with php.

    function checkWord_len($string, $nr_limit) {
    $text_words = explode(” “, $string);
    $text_count = count($text_words);
    for ($i=0; $i $nr_limit) //Check the limit
    $d = “0” ;
    return $d ; //Return the value or null

    $string_to_check = ” heare is your text to check”; //Text to check
    $nr_string_limit = ‘5’ ; //Value of limit len word
    $rez_fin = checkWord_len($string_to_check,$nr_string_limit) ;

    if($rez_fin ==’0′)
    echo “false”;
    //Execute the false code
    elseif($rez_fin == null)
    echo “true”;
    //Execute the true code


  24. BlogDemy says :

    Return value is wrong. It should return count not the array.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: