Highlight keyword or parts there of...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DynPHP
Forum Newbie
Posts: 10
Joined: Tue Mar 04, 2003 8:25 am

Highlight keyword or parts there of...

Post by DynPHP »

I have been trying all weekend to get t his to work. I am able to do a keyword search where the keyword is highlighted upon display, what I also want to be able to do is highlight parts of words or phrases that contain the keyword.

EX: if $keyword = "Windows 2000", and the search returns
Windows 2000
Windows 2000 Server
I want the "Windows 2000" portion of both highlighted, not just the "Windows 2000" one.

Right now I am using a very simple highlight function, just a simple little code string

Code: Select all

<?php
				
if ($_SESSION[keyword] == "$compatable_os" || $_SESSION[keyword] == ucfirst($compatable_os) || $_SESSION[keyword] == strtolower($compatable_os) || $_SESSION[keyword] == strtoupper($compatable_os)):
$compatable_os = "<span class = 'highlite'>$compatable_os</span>";
endif;


?>
I tried ereg_replace, but don't think I have enough experience with it to make it work properly.

Any help is greatly appreciated.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

eregi_replace is great once you get the hang of it.

Code: Select all

<?php
$string = "Windows 2000";
$replace = "<font color="#FF0000">Windows 2000</font>";
$compatible_os = eregi_replace($string, $replace, $compatible_os);
?>
DynPHP
Forum Newbie
Posts: 10
Joined: Tue Mar 04, 2003 8:25 am

Post by DynPHP »

Thx,

I actually came up with something that does exactly what I want...

Code: Select all

<?php
				
				$string = "$_SESSION[keyword]"; 
				
				$get_vendor = preg_split ("/[\s,]+/", "$vendor");
				$get_sw_name = preg_split ("/[\s,]+/", "$sw_name");
				$get_compatable_os = preg_split ("/[\s,]+/", "$compatable_os");
				$get_cto = preg_split ("/[\s,]+/", "$cto_status");
				
				$v_array = array($get_vendor);
				$s_array = array($get_sw_name);
				$c_array = array($get_compatable_os);
				$cto_array = array($get_cto);
				
				
				while (list($val) = each ($v_array)) {
				    if($val = $string):
				    $val_vendor = ucfirst($val);
				    endif;
				    
				}
				
				while (list($val) = each ($s_array)) {
				    if($val = $string):
				    $val_sw_name = ucfirst($val);
				    endif;
				    
				}
				
				while (list($val) = each ($c_array)) {
				    if($val = $string):
				    $val_compatable_os = ucfirst($val);
				    endif;
				    
				}

				while (list($val) = each ($cto_array)) {
				    if($val = $string):
				    $val_cto = ucfirst($val);
				    endif;
				    
				}
				
				$replace_v = "<span class = 'highlite'>$val_vendor</span>"; 
				$replace_s = "<span class = 'highlite'>$val_sw_name</span>"; 
				$replace_c = "<span class = 'highlite'>$val_compatable_os</span>"; 
				$replace_cto = "<span class = 'highlite'>$val_cto</span>"; 
				
				
				$vendor = eregi_replace($string, $replace_v, "$vendor"); 
				$sw_name = eregi_replace($string, $replace_s, "$sw_name"); 
				$compatable_os = eregi_replace($string, $replace_c, "$compatable_os"); 
				$cto_status = eregi_replace($string, $replace_cto, "$cto_status"); 

?>
This takes the value of the keyword and compares it to each known varible (vendor,software name, license status, and compatable os) then if the keyword value appears anywhere in these known values it is highlighted and uppercased.
Post Reply