Forum Moderators: coopster

Message Too Old, No Replies

Search Snippet Generator

         

Adrian2k4

3:49 pm on Oct 31, 2004 (gmt 0)

10+ Year Member



The Idea:
I have a site that loads all pages form a MySQL-DB.
If the user uses the site's searchfunction I'd like to include 'snippets' of the pages found in the results. (i.e. same as google)

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>","&nbsp;"), 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+

mincklerstraat

12:54 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Adrian, thanks for remembering us and posting the code. It's lookin' good! I'd imagined it would have taken a whole lot more coding than this.