Forum Moderators: coopster

Message Too Old, No Replies

Highlight dynamically generated text

         

hartzoua

1:30 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Hi,
anyone could give me a solution to the following?

I have a search form which makes queries to a database. In the form the user can search for many words (word1 && word2 && word3). I want in the results to highlight the words that the user has searched for. I 've searched in the internet and i've found some codes but none of them works with all the possible results.

What I have found so far which is closer to what I want is the following:

I create the appropriate array:
$highlight2=array(james=>yellow, span=>red,....);

I put this order in the field where the words must exist:
print(search_highlight($highlight2,$row[$i]));

and the function is:
function search_highlight($needle, $haystack)
{
foreach($needle as $key=>$val)
{

$haystack= preg_replace("/($key)/i", '<span style="background-color:'.$val.'">\\0</span>', $haystack);

}
return $haystack;
}
The problem is that when the first word is found and highlighted, the searching for the second word begins. If the second word is for example "style" it will change the word in the tag, so it is gonna become a mess!
Does anyone knows how it can ignore the tags and if possible not to be compulsory to find a word of the array?
Any advise will be invaluable. Thank you in advance

chadmg

2:09 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Good question. I was also pressed for a solution to this problem a while back. My solution was to create a key I knew would not be found. So say you want to highlight 3 words each with a different color. You create 3 different keys.

$key1 = wflkjWEFkljffce;
$key2 = wqweewqopilklkq;
$key3 = oiuevfrlklmcwpw;

Then do a search and replace with those keys. After all the highlighted words have been found you can do another search and replace, replacing the keys with html tags.

I hope there is a better solution though.

hartzoua

4:49 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Thank you chadmg,
it is a quite clever way but this is not what i am searching for. The user can search for just one letter in a specific field, so every character of the keys can match to it. Also, my search is case insensitive so i won't know exactly the initial word to replace it later.
it is possible to create a big method to have the desired result but i would prefer to find if there is a way to ignore the html tags.

chadmg

5:32 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Hmm. Good point. You could try using word boundaries if that fits what you are trying to do. You could also use randomly generated keys and check to see if the search items are found in the keys, and if so then generate another one.

I also found this code in the the php manual which should do the job much better. It processes only the parts of the text outside of markup tags. Nice.

<?php
function hilites($search, $txt) {
$r = preg_split('((>)¦(<))', $txt, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($r); $i++) {
if ($r[$i] == "<") {
$i++; continue;
}
$r[$i] = preg_replace(
"/($search)/i", "<span class='hilite'>\\1</span>", $r[$i]
);
}
return join("", $r);
}
?>

[edited by: jatar_k at 5:46 pm (utc) on July 20, 2004]
[edit reason] disabled smiles [/edit]