Megaskins Posted May 2, 2008 Share Posted May 2, 2008 Hello, hopefully someone can shed some light on what I am overlooking here. Basically this is what I am using: function wcount($str) { return count(array_filter(explode(' ', $str))); } $message = (Stuff from Mysql); $message2 = wcount($message); And then the $message gets passed onto other code to display. Now, my issue is that my amount keeps coming up 2 or more over, or 2 or more under what is actually on my screen. Now, I pretty much know its from the Forum's BBCode [blah=blah] and [/blah] codes, and/or html codes stored in people's forum posts. And possibly ,'s "'s, !'s and what not as well. And, being that I've yet to learn any Regular Expressions coding, I'm clueless where to start on trying to work around it. If anyone could give me a hand with this massive headache I've gained over the past 4 hours of trying to figure it out, I would be more than grateful. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/ Share on other sites More sharing options...
Crew-Portal Posted May 2, 2008 Share Posted May 2, 2008 have you tried... <?php function wcount($str) { return count(array_filter(explode(' ', $str))); } $message = (Stuff from Mysql); $message2 = wcount($message); echo (wcount($message)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531920 Share on other sites More sharing options...
rhodesa Posted May 2, 2008 Share Posted May 2, 2008 or better yet... $message = (stuff from mysql); print str_word_count($message); http://us2.php.net/manual/en/function.str-word-count.php Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531922 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 Yes, and yes. I've tried other snippets I found online too for instance: <?php function wordcount($str) { return count(explode(" ",$str)); } // Example Usage: $test = "This has four words."; echo "There are " . wordcount($test) . " in that sentence"; ?> and unction 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; } And the results continued to vary. I'm thinking I should start over from scratch. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531927 Share on other sites More sharing options...
benphp Posted May 2, 2008 Share Posted May 2, 2008 Your message isn't a string until you enclose it in quotes. This works for me: <?php function wcount($str) { return count(array_filter(explode(' ', $str))); } $message = "Stuff from Mysql"; $message2 = wcount($message); print "$message2"; //prints "3" ?> Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531928 Share on other sites More sharing options...
rhodesa Posted May 2, 2008 Share Posted May 2, 2008 Your message isn't a string until you enclose it in quotes. This works for me: <?php function wcount($str) { return count(array_filter(explode(' ', $str))); } $message = "Stuff from Mysql"; $message2 = wcount($message); print "$message2"; //prints "3" ?> ...they were just stating that they are getting the text from mysql... can you provide a sample block of text that is returning the wrong word count? Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531930 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 Here is a good example, color=#A68051] Tensions mounted. Moods which were mellow because of the wedding have grown ever moody. The happiness of days recently past are overcome with a shadow of ill will and it is within her journal that Ekaterina places her thoughts. Several Days Ago. [size=10][i]Joyous news! The docks were finally completed and now supplies do not have to travel across country to reach us. While the vampire kingdom to the west of us have been peaceful it is unnerving to rely on their goodwill for the necessities that sometimes must be purchased elsewhere. Debian has been in a most pleasant mood with the completion. Things have settled a bit and he has decreed that although we can not depart from his lands at this time we can, for the next week, have the honeymoon we were denied. Our first excursion, a moonlit sail complete with a basket of food and wine. Although we were under a watchful eye from the shore it seemed we were the only two around. Even in his gruffness he thinks of my happiness.[/i] [/size] Two Days Later. [size=10][i]It is destined that my husband and I shall have no lengthy honeymoon. Not two days and already there is a problem with the docks. They have been attacked and one of the smaller fishing boats of our tenants were destroyed. It was at first believed to be a vampire coven to our west but the tenants cry foul. When word is mentioned of seeking out the coven the tenants point eastward. With their repeated disavowal of it being the vampires a look to the east was taken. With this look, it was believed to be the kingdom north and eastward they spoke of however, no evidence could be found leading in that directions. The scouts, with a bit of backtracking, have found evidence that it has come due east, along the waterfront. This does not bode well.[/i][/size] Today. [size=10][i]Word has arrived that two strangers, a young woman and a masked gentleman, have been seen amongst the villagers. They grow bold in their actions by striking the women and children when answers are not hurriedly given to their questions. The villagers have come to us seeking aide in stopping these attacks. The scouts also returned with news. The attacks carry onward to the east, isolated villages were completely destroyed. As they returned, new destruction met them all along the waterfront, two more fishing boats destroyed. The tenants worry of how they shall provide for themselves as well as give tribute to our house without their boats. With Debian's permission, I have sent word seeking a boatwright so that three new boats can be built to replace the ones destroyed. Debian's mood has grown closed again as he makes ready to seek retribution. What shall the third strike of the hammer reveal?[/i][/size] Debian's call to join him leaves no chance to continue. The journal is closed then put away and the pen returned to its cradle before leaving the solace of their chambers to join Debian below.[/color] My code comes up around 505, where in fact there is around 490ish. I forgot the exact numbers since last I counted. lol Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531952 Share on other sites More sharing options...
rhodesa Posted May 2, 2008 Share Posted May 2, 2008 with this: <?php $wc_before = str_word_count($text); $wc_after = str_word_count(preg_replace('/\[.+?\]/','',$text)); print $wc_before.':'.$wc_after; ?> i get 520:505 Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531972 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 Ok, it has to be something I'm doing before it gets to there, because your code is now giving me 561. I'll start over from scratch when I get home, and see what happens then. I would give the link to the test forum in question, but I don't know what kind of trolls are lurking around here since this is my first visit to the site. Thanks for the help so far. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531996 Share on other sites More sharing options...
DarkWater Posted May 2, 2008 Share Posted May 2, 2008 I'm pretty sure that everyone here wants to help you and not go mess with your site. =/ Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-531997 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 I meant no offense, honestly. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532102 Share on other sites More sharing options...
DarkWater Posted May 2, 2008 Share Posted May 2, 2008 I know, I didn't take offense, I was just letting you know that we really wouldn't mess with your site because we know the headaches it brings. @_@ Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532104 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 I probably am being too cautious. Here is the link to the 'test' page in question. http://khaladan.com/oasis.beta/test.php?post=1485 and the code used: <?php include("db.inc"); $query = "SELECT post_text FROM nuke_bbposts_text WHERE post_id=$post"; $result0 = mysql_query($query); while(list($post_text) = mysql_fetch_row($result0)) { $wc_before = str_word_count($post_text); $wc_after = str_word_count(preg_replace('/\[.+?\]/','',$post_text)); print $wc_before.':'.$wc_after; echo "<p>"; print $post_text; } ?> Feel free to laugh at my ghetto code. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532145 Share on other sites More sharing options...
DarkWater Posted May 2, 2008 Share Posted May 2, 2008 And you're sure that 505 isn't the correct word count? Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532151 Share on other sites More sharing options...
Megaskins Posted May 2, 2008 Author Share Posted May 2, 2008 Mayhaps, but if you take a look a this one: http://khaladan.com/oasis.beta/test.php?post=1331 Its counting the - as a word as well, unless I misscounted. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532156 Share on other sites More sharing options...
rhodesa Posted May 3, 2008 Share Posted May 3, 2008 you can always try this...which will count a word as only blocks with a-z and A-Z in them... <?php $text = 'How does one go about extracting poison, etc. from glands or plants collected during quests so that it can be added to the inventory as useable? **Titled altered to reflect the question being asked. - Oasis**'; $wc_before = str_word_count($text); $wc_after = preg_match_all('/\w+/',$text,$foobar); print $wc_before.':'.$wc_after; ?> 36:35 Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532304 Share on other sites More sharing options...
sasa Posted May 3, 2008 Share Posted May 3, 2008 $wc_after = preg_match_all('/[\w\']+/s',preg_replace('/\[.+?\]/','',$post_text),$a); $wc_after = count($a[0]); Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532368 Share on other sites More sharing options...
rhodesa Posted May 3, 2008 Share Posted May 3, 2008 $wc_after = preg_match_all('/[\w\']+/s',preg_replace('/\[.+?\]/','',$post_text),$a); $wc_after = count($a[0]); I like sasa's method. The preg_match_all() function will return the number of matches though, so the second line is not needed. You do still need the 'filler' variable $a in the arguments though, cus the function requires an argument there. Quote Link to comment https://forums.phpfreaks.com/topic/103903-word-count-coming-up-with-wrong-amount-help/#findComment-532431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.