word counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
HormonX
Forum Commoner
Posts: 50
Joined: Tue Dec 10, 2002 7:43 pm
Location: Toronto

word counter

Post by HormonX »

Is there a function that would count number of words in a sentence ? If not what is the easiest way of doing that. Any help is welcome :)

Thank you.

Greg
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
function str_word_count: --> http://www.php.net/manual/en/function.s ... -count.php

djot
-
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
*great, each time uselss to post...because meanwhile someONE else posted (feyd)

:)

djot
-
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

need to get faster fingers :mrgreen:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

from reading all the user comments it seems the accuracy of this function does not seem to be all that great... the best example I could find, being:

Code: Select all

<?php 
//count words 
$words_to_count = strip_tags($body); 
$pattern = "/&#1111;^(\w|\d|''|"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/"; 
$words_to_count = preg_replace ($pattern, " ", $words_to_count); 
$words_to_count = trim($words_to_count); 
$total_words = count(explode(" ",$words_to_count)); 
?>
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
need to get faster fingers
Not really. I read the forum topics via RSS feed, and I did reduce the refresh rate to 15min because I should do other things at the moment than serving the web and answering stupid questions...


djot

PS: Anyway, I won't give up :)
-
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

djot wrote:-
...and answering stupid questions...
-
booooooooooooooo :evil:
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
boooooooooooooooooooooooo
Yes booo.


And an additionally boooooooooooooooooo from me for people that are toooooo lazy to even try to find the anwer themselves at

php.net
http://www.php.net/manual/en/ref.strings.php
(not too hard to find and to read)

or even google.
http://www.google.com/search?q=word+count+function+php
(4th position)


djot
-
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay, that's enough. Locked.
Locked