Skip to content
May 6, 2008 / ranacse05

Word Counter By PHP


I was reading Hasin Bhai’s New book Object Oriented Programming with PHP5.In chapter 2 i got a nice example of OOP.Its a word counter.Its a nice tool actually and pretty little too.Here is that example. ๐Ÿ™‚

<?
class WordCounter
{
const ASC=1;ย  //you need not use $ sign before Constants
const DESC=2;
private $words;
function __construct($filename)
{
$file_content = file_get_contents($filename);
$this->words =
(array_count_values(str_word_count(strtolower
($file_content),1)));
}
public function count($order)
{
if ($order==self::ASC)
asort($this->words);
else if($order==self::DESC)
arsort($this->words);
foreach ($this->words as $key=>$val)
echo $key .” = “. $val.”<br/>”;
}
}
?>

Advertisements

4 Comments

Leave a Comment
  1. RaiulBaztepo / Mar 29 2009 12:23 am

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ๐Ÿ˜‰
    See you!
    Your, Raiul Baztepo

  2. PiterKokoniz / Apr 8 2009 12:10 am

    Hello !!! ^_^
    I am Piter Kokoniz. Just want to tell, that your posts are really interesting
    And want to ask you: what was the reasson for you to start this blog?
    Sorry for my bad english:)
    Thank you:)
    Your Piter Kokoniz, from Latvia

    • ranacse05 / Apr 8 2009 5:06 pm

      Thanks for ur comment .
      When i stuck i found solution on the blog . So then i wanna share those solution through my blog. Got it ?

Trackbacks

  1. Word counter class | PHP Readings

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: