VERC: Generic PHP Text Search Code Last edited 5 years ago2019-04-10 09:47:08 UTC

You are viewing an older revision of this wiki page. The current revision may be more detailed and up-to-date. Click here to see the current revision of this page.
A note from the editor
This information is only retained for archival purposes and it's not recommended that you use this code in a live environment.
Someone asked how I did the filtering of articles by a search term. This is the generic function that I use to determine if a block of text does or does not meet the search requirements. It is perhaps not the most elegant code, but it does what I need it to. :)
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.

Some of the code explained:
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.

One change I should probably make to the above code is to change any instance of multiple spaces in the $search_term string to single spaces.

Hope this is of help to someone. :)
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.