Page 1 of 1

hilight search word

Posted: Mon Oct 13, 2003 6:21 am
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

Posted: Mon Oct 13, 2003 6:28 am
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>

Posted: Mon Oct 13, 2003 7:09 am
by yaron
thanks,
one problem though - is it possible to make str_replace case insensetive?

Posted: Mon Oct 13, 2003 8:06 am
by volka
unless you want to wait for str_ireplace (PHP 5 CVS only) use preg_replace() with i as option.

Posted: Mon Oct 13, 2003 8:44 am
by mudkicker
thanks volka that helped me, too! ;)