function generic_search($searchable, $search_term, $search_type)
{
if ($search_type == "phrase")
{
return eregi($search_term, $searchable);
}
elseif ($search_type == "all words")
{
$search_term = split(" ", $search_term);
foreach ($search_term as $term)
{
if (!eregi($term, $searchable)) { return FALSE; }
}
return TRUE;
}
elseif ($search_type == "any words")
{
$search_term = split(" ", $search_term);
foreach ($search_term as $term)
{
if (eregi($term, $searchable)) { return TRUE; }
}
return FALSE;
}
}
The $searchable
variable is the block of text you want to search through. $search_term
is one or more words that are being searched for in the searchable text, and $search_type
is one of either "phrase", "all words", or "any words" -- this is used to determine how the search term should be treated if it is more than one word.return eregi($search_term, $searchable);
The above line determines whether the search term, as a complete phrase, is present in the searchable text. It returns TRUE
if the string contained in $search_term
is found within the $searchable
string.
$search_term = split(" ", $search_term);
foreach ($search_term as $term)
{
if (!eregi($term, $searchable)) { return FALSE; }
}
return TRUE;
The above block of code breaks the specified search term into an array of individual words, then checks to see that each string element of that array is present in the searchable text. If one of the elements is ever not found, the function immediately returns a FALSE
value, otherwise it cycles through all of the elements and then returns TRUE
.
$search_term = split(" ", $search_term);
foreach ($search_term as $term)
{
if (eregi($term, $searchable)) { return TRUE; }
}
return FALSE;
This block is very similar to the block of code above it, except for one key difference: it's checking to see if any of the search words exist in the searchable text. The first time a word is found, the function immediately returns a TRUE
value, otherwise it cycles through all of the elements and then returns FALSE
.$search_term
string to single spaces.You must log in to post a comment. You can login or register a new account.