explode into array
Posted: Fri Jun 04, 2010 6:39 am
Hi,
I'm trying to make a search engine using full-text in boolean mode. I am using following code to hightlight the search results:
I've been trying to use explode() to split the $words= array(preg_replace('/["]/','',$searchterm2)); in witch way I would see the results in different colours, but so far I've haven't got it to work. It works if I just fill ind the words manually, like : $words= array(And, Cyrus, gave, him)
Could anybody tell me how to fix that, please? Help would be much appreciated...
I'm trying to make a search engine using full-text in boolean mode. I am using following code to hightlight the search results:
Code: Select all
$searchterm2=stripslashes($searchterm);
function highlightWords($text, $words, $colors=null)
{
if(is_null($colors) || !is_array($colors))
{
$colors = array('yellow', 'pink', 'green');
}
$i = 0;
/*** the maximum key number ***/
$num_colors = max(array_keys($colors));
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** quote the text for regex ***/
$word = preg_quote($word);
/*** highlight the words ***/
$text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
if($i==$num_colors){ $i = 0; } else { $i++; }
}
/*** return the text ***/
return $text;
}
and the following to show the results
while($row=$result->fetchRow()){
$words= array(preg_replace('/["]/','',$searchterm2));
$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);
$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);
$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);
$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);
echo '
<table style="width: 800px"><tr>
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>';
}
}I've been trying to use explode() to split the $words= array(preg_replace('/["]/','',$searchterm2)); in witch way I would see the results in different colours, but so far I've haven't got it to work. It works if I just fill ind the words manually, like : $words= array(And, Cyrus, gave, him)
Could anybody tell me how to fix that, please? Help would be much appreciated...