String search routines

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi all,

I'm trying to write a simple search routine which includes being able to
search for all words, any words and the ability to ignore results containing
a certain word, e.g.

search1 search2 : find occurences of both search1 and search2
search1+search2 : as above
search1|search2 : find occurences of either search1 or search2
search1 search2 -word : ignore results containing word

and also to have the ability to use quotes:

"search1 search2" : find occurences of search1 search2 exactly as it

or any combination of the four (e.g. "search1 search2"+search3|search4-word
which will search for search1 search2 and either search3 or search4 but not
including word).

I've managed to write a routine to find occurences of both words:

function compare_string($haystack,$needle)
 $needle=str_replace("+"," ",$needle);
 while (substr($needle,$i,1)==" ") $i++;
 $i=strpos($needle," ",$i);
 while ($i!==false)
  if (strpos($needle," ",$i)>0)
   array_push($search,substr($needle,$l+1,strpos($needle," ",$i)-$l-1));
   $i=strpos($needle," ",$i+1);
 if ($i===false) array_push($search,substr($needle,$l+1));
 for ($i=0;$i<count($search);$i++)
  if (strpos(strtoupper($haystack),strtoupper($search[$i]))!==false)
 if ($found==count($search)) return true;
 else return false;

Of course, if you change $found=0 to $found=false, $found++ to $found=true,
and remove the final if statement and put in return $found instead, then we
have a routing which finds occurences of any words (but not necessarily all)
in $needle.



Re: String search routines

Gerald Holdsworth wrote:

Quoted text here. Click to load it

Look into regular expressions, especially the preg_match() and
array_filter() functions.

Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from < (404-comp.)

Re: String search routines

Quoted text here. Click to load it

Never thought of using them in that way - will investigate and have a
further play.



Re: String search routines

Quoted text here. Click to load it

It's a pity that your data isn't held in a database table since this
is one of the things that FULLTEXT (and its non-MySQL equivalents) is
built for.

Re: String search routines

Quoted text here. Click to load it

yes - but its not the most efficient solution. OTOH its a **LOT**
better than processing every string in your dataset - you should
definitely think about storing the data in a database and doing as
much of the search as possible using SQL rather than in PHP.

Given the rather complex algebra proposed, a much simpler solution to
implementing the comparison would be to use the search phrase as a
template, and substitute 1's and 0's for matched / non-matched terms,
drop in some operators and eval the resulting string, e.g. something

$search=3Darray("flower[^\W]*", "plant[^\W]*", "\w", " ");
$target=3D"I planted a flower today because I don't like weeds";
$match_expr=3Dpreg_replace($search, $replace, $target);
// $match_expr now contains '0+1+0+1+0+0+0...

(some more thought needed on the finder points of the grammar)


Site Timeline