hilight search word

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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

hilight search word

Post by yaron »

Hello all,
I want to hilight a certain part of a string (i.e. search result)
I have both the string and the substring in different varibales,
I want to hilight the substring and then pront the entire string with the specific part hilighted .
any ideas?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this is sufficient

Code: Select all

<html>
	<head>
		<style type="text/css">
			.hilight { color: red; text-decoration: bold; }
		</style>
	</head>
	<body>
<?php
$subject = 'a teststring';
$hilight = 'test';
$output = str_replace($hilight, '<span class="hilight">'.$hilight.'</span>', $subject);
echo $output;
?>
	</body>
</html>
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

thanks,
one problem though - is it possible to make str_replace case insensetive?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

unless you want to wait for str_ireplace (PHP 5 CVS only) use preg_replace() with i as option.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

thanks volka that helped me, too! ;)
Post Reply