You are here: Re: [PHP] Breaking up search terms into an array intelligently « PHP « IT news, forums, messages
Re: [PHP] Breaking up search terms into an array intelligently

Posted by Paul Groves on 09/07/05 18:45

Gustav Wiberg wrote:
> ----- Original Message ----- From: "Paul Groves"
> <paul.groves@oucs.ox.ac.uk>
> To: <php-general@lists.php.net>
> Sent: Wednesday, September 07, 2005 4:54 PM
> Subject: [PHP] Breaking up search terms into an array intelligently
>
>
>> I want to be able to break up a number of search terms typed into an
>> input
>> box into array, simple enough one would think, just use explode, e.g
>>
>>
>> $array = explode(" ", $string);
>>
>>
>> But what if I want to be able to cope with search terms seperated by > 1
>> space (a common typing error)? This should work:
>>
>>
>> function enhanced_explode($string) {
>> $array = preg_split ("/\s+/", $string);
>> return ($array);
>> }
>>
>>
>> But what if I want to allow "Google"-type search parameters, so that
>> something like the following is split into 3 search terms?:
>> firstsearchterm "second search term" thirdsearchterm
>> The following code will do the trick, but is slow and doesn't allow for
>> multiple spaces as the delimiter, nor the possibility of multiple
>> delimiters
>> (e.g. " ", "+", "," etc.)
>>
>>
>> function explode2($delimeter, $string)
>> {
>> for ($i = 0; $i < strlen($string); $i++)
>> {
>> if ($string{$i} == '"')
>> {
>> if ($insidequotes)
>> $insidequotes = false;
>> else
>> $insidequotes = true;
>> }
>> elseif ($string{$i} == $delimeter)
>> {
>> if ($insidequotes)
>> {
>> $currentelement .= $string{$i};
>> }
>> else
>> {
>> $returnarray[$elementcount++] = $currentelement;
>> $currentelement = '';
>> }
>> }
>> else
>> {
>> $currentelement .= $string{$i};
>> }
>> }
>>
>> $returnarray[$elementcount++] = $currentelement;
>>
>> return $returnarray;
>> }
>>
>> None of these solutions are ideal, I guess a clever regular exression
>> (preg_split) could solve this, but I'm not quite sure how - does
>> anyone have
>> any ideas? Thanks
>>
>> Paul
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
>> --
>> No virus found in this incoming message.
>> Checked by AVG Anti-Virus.
>> Version: 7.0.344 / Virus Database: 267.10.18/91 - Release Date:
>> 2005-09-06
>>
>>
> Hi Paul!
>
> I guess you're looking for something like this?
>
> $sql = "SELECT ID From tbTempTable";
>
> $search = $_GET["frmSearch"];
> $search = trim($search);
> $search = strtolower($search);
>
> //Delete all occurances of " (chr(34)) in search-string
> //Delete all occurances of \ (chr(92)) in search-string
>
> $search = str_replace(chr(34),"",$search);
> $search = str_replace(chr(92),"",$search);
>
>
> //Divide search - string into an array
> //
> $searcharray = explode(" ",$search);
>
>
> //Go through search-array if several words are type
>
> //within search-string
>
> //
> $firstSQL = "WHERE";
>
> for ($i=0;$i<count($searcharray);$i++) {
>
> //Check for occuarances of +,- or "" and search in right manner...
> //
> $notset = "LIKE";
> $orset ="OR";
>
>
> //If + then don't search for +, only rest of searcharray
> //
> if (substr($searcharray[$i],0,1) == "+") {
>
> $searcharray[$i] = substr($searcharray[$i],1,
> strlen($searcharray[$i])-1);
>
> }
>
>
> //If - then don't search for +, only rest of searcharray
> //and add NOT LIKE to search-criteria
> //
>
> if (substr($searcharray[$i],0,1) == "-") {
>
> $notset = "NOT LIKE";
>
> $orset ="AND";
>
> $searcharray[$i] = substr($searcharray[$i],1,
> strlen($searcharray[$i])-1);
>
> }
>
>
> $sa = mysql_real_escape_string($searcharray[$i]);
>
>
> //Freetext search
> //
> if ($_GET["frmFreeText"] == 'on') {
>
> $sql .= " " . $firstSQL . " (tbvotes.titleJoke $notset '%" . $sa
> . "%'";
> $sql .= " $orset tbvotes.Joke $notset '%" . $sa . "%'";
> $sql .= " $orset nameOfUser $notset '%" . $sa . "%'";
> $sql .= " $orset tbvotes.nrOfVotes $notset '%" . $sa . "%'";
> $sql .= ")";
>
> }
>
>
> //Titlesearch
> //
> if ($_GET["frmTitle"] == 'on') {
>
> $sql .= " " . $firstSQL . " (tbvotes.titleJoke $notset '%" . $sa
> . "%'";
> $sql .= ")";
>
> }
>
>
> //Avs search
> //
> if ($_GET["frmFrom"] == 'on') {
>
> $sql .= " " . $firstSQL . " (tbvotes.fromJoke $notset '%" . $sa .
> "%'";
> $sql .= ")";
>
> }
>
>
> //Nr of votes - search
> //
> if ($_GET["frmVotes"] == 'on') {
>
> $sql .= " " . $firstSQL . " (tbvotes.nrOfVotes $notset '%" . $sa
> . "%'";
> $sql .= ")";
>
> }
>
>
> //Grade - search
> //
> if ($_GET["frmGrade"] == 'on') {
>
> $sql .= " " . $firstSQL . " ((totalValueJoke / nrOfVotes) $notset
> '%" . $sa . "%'";
> $sql .= ")";
>
> }
>
>
>
> //Set AND if more than word typed in...
> //
> $firstSQL = "AND";
>
>
>
> }
>
>
>
> }
> //END Search now...
> //
>
> //echo $sql;
>
> I hope it will give you a clue into right direction... :-)
>
> /G
> http://www.varupiraten.se/
>
>
>

Thanks - this helps to an extent (espc. with how to deal with "+" and
"-" delimiters), but doesn't (I think) solve the problem of how to treat
parts of search terms in quotes as single search terms (like Google
does), espc. as quotes get stripped out in your example. Nor does it
deal with the problem of muliple spaces between search terms, though
that's relatively easy to fix using the enhanced_explode() function above.

regards

Paul

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация