PHP Classes

File: word_count.php

Recommend this page to a friend!
  Classes of Rahul   Word Count   word_count.php   Download  
File: word_count.php
Role: Class source
Content type: text/plain
Description: php file containg a class and method usage
Class: Word Count
Find the word that appears more times in a string
Author: By
Last change: none
Date: 15 years ago
Size: 778 bytes
 

Contents

Class file image Download
<?php


/* * * *

Author: Rahul Sinha
email: rahul@zimaudio.com
Class: Class to display the word that has been used max times in the input string.
Usage: Free
input: string
exp:
$str = "This is a test for a test scenario.";
$obj = new WordCount;
print $obj->countWords($str); // test
* * * */

class WordCount{
   
    private
$str;
   

    function
__construct()
    {
       
$this->str = "Nothing Supplied";
       
    }

    function
countWords($str)
    {
       
       try
        {
            if(empty(
$str))
            {
                throw new
Exception("$this->str");
            }
           
$this->str = $str;
           
$cnt = str_word_count($this->str,2);
           
$wrd = $cnt[max(array_keys($cnt))];
        }
        catch (
Exception $e) {
            echo
'Caught exception: ', $e->getMessage(), "\n";
        }
        return
$wrd;
    }

}
?>