Re: highlighting multi term search results

From: Date: Mon, 08 Sep 2003 09:19:50 +0000
Subject: Re: highlighting multi term search results
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-161965@lists.php.net to get a copy of this message
Am Montag, 08.09.03 um 04:21 Uhr schrieb Lee O'Mara:
Why not just allow for bold tags in the search term? What I mean is, I think you can get the results you want by allowing any number of open or close bold tags between each letter of the search term.
i thought so too (but i had no idea how to code it); this must be a solution. i've tested your function like this: <? function highlight($src_terms, $src_terms_int, $result) { $i = 0; while ($i < $src_terms_int) { $termWithOptionalBold = preg_replace('/(.)/', '(<\/?b>) *\1',$src_terms[$i]); $result = preg_replace('/(<\/?b>)*('.$termWithOptionalBold.')(<\/?b>) */si', '<b>\2</b>', $result); $i++; } return $result; } $search = "te est"; // user input to search for $src_terms = explode(" ", $search); $src_terms_int = count($src_terms); $result = "this is just a test"; // result from database print highlight($src_terms, $src_terms_int, $result); ?> but it didn't highlight anything. btw, the bold tag in this example is used to simplify it and stands for something like <span class="highlight"></span> what makes it even trickier, i guess. thanks for your help! jns

« previous php.general (#161965) next »