Re: Faking Boolean

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 11:08 PM 4/5/2006, John Taylor-Johnston wrote:
How can I take enquiry:

<input name="searchenquiry" type="text" value="john">
or
<input name="searchenquiry" type="text" value="john johnston">

and parse its value to come up with this to do a boolean search

+"john"
or
+"john" +"johnston"


John,

If you're splitting your search string into discrete words, most search engine logic doesn't require quotes. Typically, quotation marks combine multiple words into single expressions, which makes sense with "John Johnston" but not with "John" "Johnston".

But since you requested the quotes I'll include them:

Here's one way to convert an incoming word series for a search:

        // get the entered text
        $sEnquiry = $_GET["searchenquiry"];

RESULT: [ john    johnston ]

        // protect against input attacks here
        ...

        // remove whitespace from beginning & end
        $sEnquiry = trim($sEnquiry);

RESULT: [john    johnston]

        // replace internal whitespace with single spaces
        $sEnquiry = preg_replace("/\s+/", " ", $sEnquiry);

RESULT: [john johnston]

        // replace each space with quote-space-plus-quote
        $sEnquiry = str_replace(" ", "\" +\"", $sEnquiry);

RESULT: [john" +"johnston]

        // add beginning & ending delimiters
        $sEnquiry = "+\"" . $sEnquiry . "\"";

RESULT: [+"john" +"johnston"]


Another technique that's worth exploring uses explode & implode to isolate the words and the join them again with different delimiters, e.g.:

        // remove extraneous whitespace
        $sEnquiry = ...

        // split string into array of words on each space
        $aWords = explode(" ", $sEnquiry);

RESULT: array(
                john
                johnston
        )

        // concatenate the array back into a string using desired delimiters
        $sSearch = implode(" +", $aWords);

RESULT: [john +johnston]

Paul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux