Location: PHPKode > scripts > Words counter > class.wordcounter.php
<?php
/*
   

*/
class WordCounter
{ 
   private $k=false;
   private $words;
   /////////////////////////////////main counter////////////////////////////
    function __construct($filename,$casecheck) 
	{
	   $file_content = file_get_contents($filename);
	   if($casecheck==0)
	   {
	     $this->words = (array_count_values(str_word_count($file_content,1)));
		 $this->twords = ( str_word_count($file_content,1));
		}
		else
		{
		   $this->words = (array_count_values(str_word_count(strtolower ($file_content),1)));
		   $this->twords = ( str_word_count(strtolower ($file_content),1));
		} 		  
		
		$unique_words = count($this->words);
		$total_words = count($this->twords);
		echo "<br/><font style='color:#CC0000; font-weight:bold'>Unique Words = ".$unique_words."</font>";
		
		echo "<br/><font style='color:#CC0000; font-weight:bold'>Total words = ".$total_words."</font></br>";
   }
		 
		 
    ////////////////////////////////Show list by  order//////////////////////////////
	
	  
	  public function checkOrder($orderby)
	  {
	    if($orderby==0) ksort($this->words);
		else if($orderby==1) krsort($this->words);
		
		foreach ($this->words as $key=>$val)
		    echo $key ." = ". $val."<br/>";
	  
	  
	  }
	  
	 
	  //////////////////////search word ////////////////////////////////
	   public function searchWord($search,$casecheck)
	  {
	      if($casecheck==1)
		   {
		      $search=strtolower($search);
		   }
		   else
		   $search=$search;
	            
	   foreach ($this->words as $key=>$val)
	   {
		  
			if($key==$search)
			{
		 	 $k=true;
			  echo "<font style='color:#CC0000; font-weight:bold'>search word is --->> ".$key ." = ". $val."</font><br/>";
			}
			
	   }
	   if($k==false)
	    echo "<font style='color:#CC0000; font-weight:bold'>word not found</font><br/>";
	 
	 } 
	  
	  
	
}
?>

Return current item: Words counter