Location: PHPKode > scripts > Word Count > word-count/word_count.php
<?php


/* * * * 

Author: Rahul Sinha
email: hide@address.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;
	}

}
?>
Return current item: Word Count