Jump to content

Word count coming up with wrong amount [Help]


Megaskins

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.