Welcome Guest, Not a member yet? Register   Sign In
Count words in Div
#1

[eluser]Perkin5[/eluser]
How would I set about counting words in a div?

Counting words in a string is straightforward but how do I make the string equal the contents of the div?
#2

[eluser]John_Betong_002[/eluser]
What is the source for the div contents?

Can you give an example?
 
 
#3

[eluser]PhilTem[/eluser]
Hm... I'd suggest using

Code:
preg_match_all($pattern, $div_string, $matches);

with the correct pattern that also matches some attributes like 'class' or 'id' within '<div>' if you have these (I'm not good at regexp-patterns, better ask somebody else Wink ) and then use

Code:
count($matches);

to count the amount of matched items i.e. words.

But what exactly is your definition for 'word'? Is it a random string pattern surrounded by white-spaces? Or must it be a word that can be found within a dictionary? So to speak: 'Real words' or only 'character combinations surrounded by white-spaces'? What's your use case?
#4

[eluser]Perkin5[/eluser]
My div would be like this:

Code:
<div id="somediv">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
&lt;?php echo $word_count?&gt;

where $wordcount is a function that counts the words in the div.

I'm aware of things like:
Code:
function wordcount($str) {

  return count(explode(" ",$str));

}


but how do I make $str equal to the div contents?
#5

[eluser]John_Betong_002[/eluser]
I fail to understand how the string of words get into the DIV.

Code:
&lt;?php
        $str =
        "
         Pellentesque habitant morbi tristique senectus et
         netus et malesuada fames ac turpis egestas.
         Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.
         Donec eu libero sit amet quam egestas semper.
         Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
        ";
      ?&gt;
  <div id="somediv">
    <p>
      &lt;?php echo $str;?&gt;
    </p>
    &lt;?php echo 'Word count: ' .str_word_count($str,0); ?&gt;
  </div>
&nbsp;
&nbsp;
#6

[eluser]Perkin5[/eluser]
OK, now it is confirmed...I am definitely stupid!

Thank you very much for leading the challenged.

I had visions of something like $str = $css_str("#somediv"); etc etc...sad.
#7

[eluser]John_Betong_002[/eluser]
No problem we all have our off-days Smile

My ongoing problem is to try and wrap my head around global time zones for forum posts, emails, FaceBook, Google Webmaster Tools... the bl@@dy list gets longer every day Sad
&nbsp;
&nbsp;
#8

[eluser]Perkin5[/eluser]
And they say time is the great healer!




Theme © iAndrew 2016 - Forum software by © MyBB