<? 
class HighLightText
{ 
	// these two consecutive functions are useful for search and customize the found text . 
	public function matchAndReplaceText($keyword,$string ,$condition=NULL)
	{ 
		//if the search Keyword is empty then return with the string  , do not go any further 
		if(empty($keyword))
		{ 
			return $string; 
		}
		//omit any html tags if present other wise it will create conflict.
		$string = strip_tags($string); 
		// to check if the search keyword contains space ,it breaks into the words and search for each words.  
		$keyword= $this->getArrayofStringWithNoSpace($keyword);
		
			// above function return string either in array or string. 
		if(is_array($keyword))
		{ 
			
			
			// replaces the main string with the all the words return by the array of the above functions . 
			foreach($keyword as $text)
			{ 
					
					if(eregi($text,$string))
					{ 
						$string = eregi_replace($text,"<span class='foundText'>".$text."</span>",$string); 
					} 
			} 
			return $string; 
		} 
		// if the above function returns the string then it only check the string with the returned string. 
		else
		{ 
			
			if(eregi($keyword,$string) and !empty($keyword))
				return eregi_replace($keyword,"<span class='foundText'>".$keyword."</span>",$string); 
			else 
				return $string; 
		} 
	} 
	// this function breaks the string by words by finding the whitespace and returns all the words into the array . 
	 private  function getArrayofStringWithNoSpace($string)
	{ 
		// checks if the string contains whitespace.
		$newArray = array(); 
		if(eregi(' ',$string))
		{ 
			
			//splits the string by whitespace and returns into array
			$ar =  explode(' ',$string); 
			
			foreach($ar  as $val)
			{ 
				// checks if the array value contais words or white space 
				if(!empty($val))
					$newArray[] = $val; 
			}  
			return $newArray; 	
				
		
		}
		else 
		{ 
			$newArray[] = $string; 
			return $newArray; 
		}  
	
	} 
} 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HighLight Matched Text</title>
<style> 
span.foundText { background:#990000;color:white } 
</style> 
</head>
<body style="font-family:'Segoe UI'">
<div align="center"> 
	<div style='width:800px ;'>
	
		<div style="padding:5px;margin:5px;background-color:#F2F2F2;border:1px solid #D3D3D3"  align="left"> 
			<form method="get"> 
				<table width="100%"> 
					<tr> 
						<td width="20%">Search Keyword: </td> 
						<td width="22%"><input type="text" 	 name="keyword" value="lorem has" > </td> 
					  <td width="58%"><input type="submit" name="submit" value="Search and Highlight" ></td> 
					</tr>
				</table>
			</form> 
		</div> 
		<div style="padding:5px;margin:5px;background-color:#F2F2F2;border:1px solid #D3D3D3"  align="left"> 
			<p> 
            	
				<?
					$string =  <<<EOT
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
EOT;
				 
				  
				  $ht =  new HighLightText(); 
				  echo $ht->matchAndReplaceText($_GET['keyword'] , $string ); 
				 ?>
			</p> 
		</div> 
	</div>
	
</div> 

</body>
</html>