Welcome! Log In Create A New Profile

Advanced

Search for part of a word by means of * wildcards

Posted by Tec 
Tec
Search for part of a word by means of * wildcards
November 12, 2007 11:36PM
This mod enhances Sphiders capabilities to search also for parts of a word. You may use * wildcards like
*searchme
*searchme*
searchme*
The query search*me will be rejected. With some small changes this mod could be modificated to search for parts of a word also without any wildcard. But to search for parts of a word requires an additional mySQL request. So, as this would slow down every search enquiry, I decided for the first issue to include the *wildcards.


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

while (($words < count($wordarray)) && $possible_to_find == 1) {
if ($stem_words == 1) {
$searchword = addslashes(stem($wordarray[$words]));
} else {
$searchword = addslashes($wordarray[$words]);
}
$wordmd5 = substr(md5($searchword), 0, 1);
$query1 = "SELECT distinct link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword' $domain_qry order by weight desc";
echo mysql_error();
$result = mysql_query($query1);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
if ($type != "or"winking smiley {
$possible_to_find = 0;
break;
}
}
if ($type == "or"winking smiley {
$indx = 0;
} else {
$indx = $words;
}

while ($row = mysql_fetch_row($result)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
$words++;
}

Delete all that and replace it with the following:

// If * wildcard, search for part of a word
$wild_correct = 0;
$searchword = addslashes($wordarray[$words]); // get only first word of search query
$wildcount = substr_count($searchword, '*');
if ($wildcount == 1 xor $wildcount ==2) { // only one or two * are allowed
$length = strlen($searchword);
$first = strpos($searchword, '*');
$last = strrpos($searchword, '*');

if ($wildcount == 1) { // if we have only one *
if ($first == 0) $wild_correct = '1a'; // * must be always in pos 0
if ($last == ($length-1)) $wild_correct = '1b'; // or alternate in last position
}

if($wildcount == 2) { // if we have two *
if ($first == 0 && $last == ($length-1)) $wild_correct = '2'; // they must be in first and last position
}

if ($wild_correct != 0) {
$searchword = str_replace('*','%', $searchword);
$recovered = str_replace('%', '',$searchword);
$wordarray[0]= $recovered;
$searchstr['hilight'][0] = $recovered;
$searchstr['+'][0] = $recovered;

$query = "SELECT keyword_id, keyword from ".$mysql_table_prefix."keywords where keyword like '$searchword'";
echo mysql_error();
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

if ($num_rows == 0) { // if there was no searchword in table keywords
$possible_to_find = 0;
$break = 1;
}
if ($num_rows !=0) {
for ($i=0; $i<$num_rows; $i++) { // get all searchwords as keywords from table keywords
$keyword_id = mysql_result($result, $i, "keyword_id"winking smiley;
$keyword = mysql_result($result, $i, "keyword"winking smiley;

$wordmd5 = substr(md5($keyword), 0, 1); // calculate attribute for link_keyword table
$query1 = "SELECT link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5 where keyword_id = '$keyword_id' order by weight desc";
echo mysql_error();
$reso = mysql_query($query1);
$lines = mysql_num_rows($reso);

if ($lines != 0) {
$indx =$words;
}

while ($row = mysql_fetch_row($reso)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
}
}
}
} else { // standard search without wildcard
while (($words < count($wordarray)) && $possible_to_find == 1) {
if ($stem_words == 1) {
$searchword = addslashes(stem($wordarray[$words]));
} else {
$searchword = addslashes($wordarray[$words]);
}
$wordmd5 = substr(md5($searchword), 0, 1);
$query1 = "SELECT distinct link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword' $domain_qry order by weight desc";
echo mysql_error();
$result = mysql_query($query1);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
if ($type != "or"winking smiley {
$possible_to_find = 0;
break;
}
}
if ($type == "or"winking smiley {
$indx = 0;
} else {
$indx = $words;
}

while ($row = mysql_fetch_row($result)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
$words++;
}
}


In the same file search for:

$page_size = number_format($page_size, 1)."kb";

Beyond that row additionally include the following:

// Enable part of a word highlighting in result report
$recovered = str_replace('*', '',trim($query));
$words['hilight'][0] = $recovered;
$words['+'][0] = $recovered;


Happy coding

Tec
Re: Search for part of a word by means of * wildcards
November 13, 2007 02:47PM
How hard would it be to get search*me to work. Next version???
Tec
Re: Search for part of a word by means of * wildcards
November 14, 2007 12:17PM
Version 2
Advantages on version 1:
1. Search requests like search*this and *search*more* are also supported.
2. Search report now highlights the complete word found in Sphiders database, not only the part of the words you entered into the search field.


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

while (($words < count($wordarray)) && $possible_to_find == 1) {
if ($stem_words == 1) {
$searchword = addslashes(stem($wordarray[$words]));
} else {
$searchword = addslashes($wordarray[$words]);
}
$wordmd5 = substr(md5($searchword), 0, 1);
$query1 = "SELECT distinct link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword' $domain_qry order by weight desc";
echo mysql_error();
$result = mysql_query($query1);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
if ($type != "or"winking smiley {
$possible_to_find = 0;
break;
}
}
if ($type == "or"winking smiley {
$indx = 0;
} else {
$indx = $words;
}

while ($row = mysql_fetch_row($result)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
$words++;
}


Delete all that and replace it with the following:


//find all sites that include the search word
$wordarray = $searchstr['+'];
$words = 0;
$starttime = getmicrotime();

//**** If * wildcard, search for part of a word
$wild_correct = 0;
$searchword = addslashes($wordarray[$words]); // get only first word of search query
$wildcount = substr_count($searchword, '*');

if ($wildcount) { // if * wildcard , enter here
$searchword = str_replace('*','%', $searchword);
$query = "SELECT keyword_id, keyword from ".$mysql_table_prefix."keywords where keyword like '$searchword'";
echo mysql_error();
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

if ($num_rows == 0) { // if there was no searchword in table keywords
$possible_to_find = 0;
$break = 1;
}
if ($num_rows !=0) {
global $all_wild;
$all_wild = '';
for ($i=0; $i<$num_rows; $i++) { // get all searchwords as keywords from table keywords
$keyword_id = mysql_result($result, $i, "keyword_id"winking smiley;
$keyword = mysql_result($result, $i, "keyword"winking smiley;
$all_wild =("$all_wild $keyword"winking smiley;

$wordmd5 = substr(md5($keyword), 0, 1); // calculate attribute for link_keyword table
$query1 = "SELECT link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5 where keyword_id = '$keyword_id' order by weight desc";
echo mysql_error();
$reso = mysql_query($query1);
$lines = mysql_num_rows($reso);

if ($lines != 0) {
$indx =$words;
}

while ($row = mysql_fetch_row($reso)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
}
}

} else { // if no wildcard, enter here
while (($words < count($wordarray)) && $possible_to_find == 1) {
if ($stem_words == 1) {
$searchword = addslashes(stem($wordarray[$words]));
} else {
$searchword = addslashes($wordarray[$words]);
}
$wordmd5 = substr(md5($searchword), 0, 1);
$query1 = "SELECT distinct link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword' $domain_qry order by weight desc";
echo mysql_error();
$result = mysql_query($query1);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
if ($type != "or"winking smiley {
$possible_to_find = 0;
break;
}
}
if ($type == "or"winking smiley {
$indx = 0;
} else {
$indx = $words;
}

while ($row = mysql_fetch_row($result)) {
$linklist[$indx]['id'][] = $row[0];
$domains[$row[0]] = $row[2];
$linklist[$indx]['weight'][$row[0]] = $row[1];
}
$words++;
}
} // ***** end


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, $all_wild,


In the same file search for:

$page_size = number_format($page_size, 1)."kb";

Beyond that row additionally include:

// If available, enable part of a word highlighting in result report
if ($all_wild) $words = makeboollist($all_wild);


Happy coding

Tec
DoN
Re: Search for part of a word by means of * wildcards
January 24, 2008 08:58AM
awesome mod good work Tec !

greetings
Re: Search for part of a word by means of * wildcards
February 10, 2008 07:02AM
AWESOME!!! Thanks.
Re: Search for part of a word by means of * wildcards
May 02, 2012 01:20PM
Had not seen this one before.

Just added it to my site and works nicely.

Thanks Tec!smileys with beer
Re: Search for part of a word by means of * wildcards
August 12, 2012 02:03AM
This code is amazing and works perfectly. I saw it before but didn't realize it was so easy to implement. Thanks Tec.
Sorry, only registered users may post in this forum.

Click here to login