CodeIgniter Forums
Count words in Div - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Count words in Div (/showthread.php?tid=48509)



Count words in Div - El Forum - 01-19-2012

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


Count words in Div - El Forum - 01-19-2012

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

Can you give an example?
 
 



Count words in Div - El Forum - 01-19-2012

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


Count words in Div - El Forum - 01-19-2012

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



Count words in Div - El Forum - 01-19-2012

[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;


Count words in Div - El Forum - 01-19-2012

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


Count words in Div - El Forum - 01-23-2012

[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;



Count words in Div - El Forum - 01-23-2012

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