Tec
Enable strict search by means of !searchstring.
December 03, 2007 09:28PM
This mod enhances Sphiders capabilities to search strictly for the search query.

Quote recortes:
<<< I am looking for the word "REN", but I get "REN" and also "impRENsa", "pRENsa", compRENsion, etc. >>>

The mod was designed to prevent this behaviour. It will present only the results for "REN" at Sphiders result pages. So it is the reverse function of "Search for part of a word by means of * wildcards" http://www.sphider.eu/forum/read.php?3,2585
Invoke this mod by entering a ! as first character of your search query.
If the search term will be found only in title, url or meta tags but not in the HTML body, a corresponding message will be displayed. After installing this mod the message will also appear if you do a standard search without invoking the !strict search.


In .../include/searchfuncs.php search for:

$searchword = addslashes($wordarray[$words]);
}

Beyond these rows additionally include:

$strictpos = strpos($searchword, '!'); // if ! is in position 0, we have to search strict
if ($strictpos === 0) $searchword = str_replace('!', '', $searchword);



In the same file search for:

global $sph_messages, $results_per_page,

Delete that row and replace it with:

global $sph_messages, $results_per_page, $show_meta_description,


In the same file search for:

$txtlen = strlen($fulltxt);
if ($txtlen > $desc_length) {
$places = array();
foreach($words['hilight'] as $word) {
$tmp = strtolower($fulltxt);
$found_in = strpos($tmp, $word);
$sum = -strlen($word);
while (!($found_in =='')) {
$pos = $found_in+strlen($word);
$sum += $pos; //FIX!!
$tmp = substr($tmp, $pos);
$places[] = $sum;
$found_in = strpos($tmp, $word);

}
}

Delete all that and replace it with the following:

$txtlen = strlen($fulltxt);
$refreshed = ereg_replace("[*!]", '',trim($query)); // works also for *wildcard search
if ($show_meta_description === 1 || $txtlen > $desc_length) {
$places = array();
$strictpos = strpos($query, '!');

if ($strictpos === 0) { // if !strict search enter here
$recovered = str_replace('!', '',trim(strtolower($query)));
$words['hilight'][0] = $recovered; // replace without ' ! '
$strict_length =strlen($recovered);

$tmp = strtolower($fulltxt);
$found_in = '1'; // pointer position start

foreach($words['hilight'] as $word) {
$sum = -strlen($word);
while (!($found_in =='')) {
$found_in = strpos($tmp, $word);
$tmp_front = substr($tmp, $found_in-1); // one character before found match position
$pos = $found_in+strlen($word);
$sum += $pos;
$tmp = substr($tmp, $pos); // get rest of fulltxt
// check weather found match is realy strict
$found_before = preg_match("/[-_*\:&@\w]/", substr($tmp_front, 0, 1));
$found_behind = preg_match("/[-_*\:&@\w]/", substr($tmp, 0, 1));

if ($found_before ===0 && $found_behind ===0) {
$places[] = $sum; // remind position of match
}
$found_in = strpos($tmp, $word); // new position of pointer
if ($places[0] != '') $found_in = ''; // end if first place was found
}
}
} else { // if not !strict search enter here (standard Sphider)
foreach($words['hilight'] as $word) {
$tmp = strtolower($fulltxt);
$found_in = strpos($tmp, $word);
$sum = -strlen($word);
while (!($found_in =='')) {
$pos = $found_in+strlen($word);
$sum += $pos; //FIX!!
$tmp = substr($tmp, $pos);
$places[] = $sum;
$found_in = strpos($tmp, $word);
}
}
}

In the same file search for:

if ($title=='')
$title = $sph_messages["Untitled"];
$regs = Array();
foreach($words['hilight'] as $change) {
while (@eregi("[^\>](".$change."winking smiley[^\<]", " ".$title." ", $regs)) {
$title = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $title);
}

while (@eregi("[^\>](".$change."winking smiley[^\<]", " ".$fulltxt." ", $regs)) {
$fulltxt = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $fulltxt);
}
$url2 = $url;
while (@eregi("[^\>](".$change."winking smiley[^\<]", $url2, $regs)) {
$url2 = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $url2);
}
}

if (strlen($title) > 80) {
$title = substr($title, 0,75)."...";
}

Delete all that and replace it with the following:

if ($title=='')
$title = $sph_messages["Untitled"];
$regs = Array();
if (strlen($title) > 80) {
$title = substr($title, 0,75)."...";
}
$url2 = $url;
if ($places[0] == '') { // if no HTML text to highlight
if ($show_meta_description === 1) $fulltxt = "<font color=\"red\">Search string '$refreshed' only found in title or url.</font>";
else $fulltxt = "<font color=\"red\">Search string '$refreshed' only found in title, url or meta tags.</font>";
$words['hilight'][0] = $refreshed;
}

foreach($words['hilight'] as $change) {
if (!($strictpos === 0)) {
while (@eregi("[^\>](".$change."winking smiley[^\<]", " ".$title." ", $regs)) {
$title = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $title);
}
while (@eregi("[^\>](".$change."winking smiley[^\<]", $url2, $regs)) {
$url2 = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $url2);
}
}
while (@eregi("[^\>](".$change."winking smiley[^\<]", " ".$fulltxt." ", $regs)) {
$fulltxt = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $fulltxt);
}
}
$places = array();


Happy coding

Tec
Re: Enable strict search by means of !searchstring.
December 03, 2007 11:35PM
Thanks, Tec.
It works OK.

Key words for some one else:
"Wordbreaker", "Stemmer" and "Stop List"
Sorry, only registered users may post in this forum.

Click here to login