Page 1 of 1

eregi_replace problem

Posted: Mon Oct 27, 2003 10:52 am
by yaron
hello all,
I try to use eregi_teplace in order to hilight search words.
here is my command:

Code: Select all

<?php
$hilighted=eregi_replace($sWord,"<SPAN style='background:yellow;'>$sWord</SPAN>",$resArr['field']);

?>
the hilighting is ok but eregi changes my search word ($sWord) to lower or upper case (depends on how the search word was typed).
What I want to do is to be able to find the search word regardless of the case (that is why i use eregi_replace) but to display the search word (hilighted) in the original form (lower or higher case)
any ideas?

Posted: Mon Oct 27, 2003 11:29 am
by volka
use a subpattern for replacement

Code: Select all

<?php
$subject = 'A preg_replace Test';
$sWord = 'PREG';
$hilighted=preg_replace('!('.$sWord.')!i',"<SPAN style='background:yellow;'>\\1</SPAN>",$subject); 
echo $hilighted;
?>

Posted: Mon Oct 27, 2003 12:00 pm
by m3rajk
volka wrote:use a subpattern for replacement

Code: Select all

<?php
$subject = 'A preg_replace Test';
$sWord = 'PREG';
$hilighted=preg_replace('!('.$sWord.')!i',"<SPAN style='background:yellow;'>\\1</SPAN>",$subject); 
echo $hilighted;
?>
i ronically, "" are allowed to quote a pattern (that or i have a forgiving set up) and you can use "/($sWord)/i" in place of '!('.$sWord.')!i'

(btw: in the case you're importing the string from someplace else, the delimiter is completely irrelevant.)

Posted: Mon Oct 27, 2003 12:14 pm
by volka
i ronically, "" are allowed to quote a pattern
of course they are, it's a literal like any other.
But if I have to chose between

Code: Select all

$pattern = '!('.$sWord.')!i';
// and 
$pattern = "!($sWord)!i";
I'll take the first one. I really love syntax highlight ;)

Posted: Mon Oct 27, 2003 12:23 pm
by m3rajk
lol. i find the second easier to type and go with that...

Posted: Tue Oct 28, 2003 4:00 am
by yaron
thanks! it's working fine