Re: highlighting multi term search results
| From: | Catalin Trifu | Date: | Sun, 07 Sep 2003 12:11:02 +0000 |
| Subject: | Re: highlighting multi term search results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-161902@lists.php.net to get a copy of this message | ||
Hi,
> <?
> function highlight($src_terms, $src_terms_int, $result) {
> $i = 0;
> while ($i < $src_terms_int) {
> $result = preg_replace('/('.$src_terms[$i].')/si',
> '<b>'.$src_terms[$i].'</b>', $result);
Here the $result is changed to '<b>te</b>st' on the first search.
Obviously on the second replace the term will not be found anymore!
> $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);
> ?>
Cheers,
Catalin