Half-Life Re-imagined competition results on their way...
Check out Skewing textures in Hammer, our newest tutorial!
Say hello to MD_AZMAL, our newest member!

logo

Site Stuff

Reference

Maps

Community

ShoutBOX

Poll

Feeling Blue

What's your favourite shade of blue?

Azure

8

Cobalt

18

Turquoise

4

Cyan

8

Royal

3

Teal

3

Onliners

2 mins

Windawz

2 mins

Penguinboy

8 mins

Solokiller

10 mins

Silvertongue

16 mins

Instant Mix

43 mins

Strider

51 mins

abbadon

Affiliates

A gaming and technology blog by TWHL admins Penguinboy and Ant. A music blog by TWHL users Ant and Hugh.

Generic PHP Text Search Code

By Chris 'autolycus' Bokitch

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. :)