Forum Moderators: coopster
I asked the forum a while ago on how to accomplish this:
[webmasterworld.com...]
In the meantime i scripted something similar to the Ideas in the earlier post. The algo i now use is simpler but I figured it's good enough.
Here's the function:
<?php
function snippet($content, $mysql_formatted_searchquery) {
$mysql_formatted_searchquery = str_replace("_", "%", $mysql_formatted_searchquery);
$mysql_formatted_searchquery = explode("%", $mysql_formatted_searchquery);
$searchquery = array();
foreach ($mysql_formatted_searchquery as $id => $value) {
if (strlen($value)) {
$searchquery[] = strtolower($value);
}
}
$content = str_replace(array("</td>","</h1>"," "), array("<br>", "<br>", " "), $content);
$content = strip_tags($content, "<br>");
$content = ereg_replace("[[:space:]]+", " ", $content);
$content = explode("<br>", $content);
$content_count = array();
foreach ($content as $id => $value) {
$value = strtolower(trim($value));
$count = 0;
foreach ($searchquery as $searchstring) {
$count += substr_count($value, $searchstring);
}
$content_count[$id] = $count;
}
arsort($content_count);
if ($content_count[key($content_count)] > 0) {
$output = '<div style="padding:10px; font-weight:normal;">';
$snippet = trim($content[key($content_count)]);
$i = 1;
while (strlen($snippet) < 200 and isset($content[key($content_count)+$i])) {
$snippet .= '<br>'.trim($content[key($content_count)+$i]);
$i++;
}
foreach($searchquery as $searchstring) {
$snippet = str_replace($searchstring, '<font color="#FF0000">'.$searchstring.'</font>', $snippet);
}
$output .= $snippet.'</div>';
return $output;
} else {
return "<br>";
}
}
?>
Input:
$content - String of the Page (i.e. HTML-code of the Page found)
$mysql_formatted_searchquery - String used to search MySQL (i.e. "%foo%" or "%foo_bar%" etc.)
Known Bugs:
Highlighting is case-sensitive. I would need some kind of regex to make the replace caseinsensitive.
The whole thing isn't very optimised (consider it a alfa version) - Parsing takes approx. 1ms per result on my AMD AthlonXP 2600+