Click to See Complete Forum and Search --> : preg_replace() help


CWCJimmy
04-21-2006, 10:10 AM
$titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'field1' ] );
$linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'field2' ] );
$linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'field3' ] );

foreach($trimmed_array as $trimm){
if($trimm != 'b' ){
//IF you added more fields to search make sure to add them below as well.
$titlehigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $titlehigh);
$linkhigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkhigh);
$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc);



This code is a snippet from this page: http://www.devpapers.com/article/306

I'm trying to write a searching component for my site in PHP. I know the idea is to search the values pulled from the database and "highlight" the keywords input into the search box, but I don't understand how this is doing it. I'm unfamiliar with the 'si' and the '\\1' pieces in this code and can't seem to find much on them.

Can anyone explain or direct me to an explanation?

Thanks!

LiL|aaron
04-21-2006, 10:19 AM
Have a look here

http://uk2.php.net/manual/en/function.preg-replace.php

bokeh
04-21-2006, 10:21 AM
si is infact "s" and "i". "i" is case insensitive and "s" allows ".+" to match charcters that include a line break. \1 is a back reference to the first bracketed part of the expression and a second "\" is needed as an escape character because the group is contained within a double quoted string literal.

CWCJimmy
04-21-2006, 10:33 AM
Aaron, that's the first thing I checked but it doesn't really help. I understand what the intent of the function is, I just don't get some of the parameters that can be assigned to it. For instance the 'si' in the code above and in one of the examples they use "@<[\/\!]*?[^<>]*?>@si'" as a parameter which supposedly identifies the html tags in a string. This doesn't make sense to me. It looks like gibberish.

Thank you for your response though!

CWCJimmy
04-21-2006, 10:34 AM
Thanks Bokeh!