VERC: Generic PHP Text Search Code Last edited 16 years ago2002-11-11 18:09:00 UTC by Penguinboy Penguinboy

This article was recovered from an archive and needs to be reviewed

  1. The formatting may be incorrect as it was automatically converted to WikiCode from HTML, it needs to be revised and reformatted
  2. Some information may be out of date as it was written before Half-Life was available on Steam
  3. After the article is re-formatted and updated for Steam HL, remove this notice
  4. Please do not remove the archive notice from the bottom of the article.
  5. Some archive articles are no longer useful, or they duplicate information from other tutorials and entity guides. In this case, delete the page after merging any relevant information into other pages. Contact an admin to delete a page.
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.