Hi Matt,

The code I use isn't really standalone, but here are a few snippets that 
might help. This is assuming you go down the URL rewriting road, which 
is something I would recommend (but the same concepts apply either way)...

note some of these bits of code I just assembled now and might have bugs 
or need tweaking etc.

# .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^search/.*$ search.php # send all search requests to 
search.php - not fussed about setting querystring variables here


// search.php

//If the form has been POSTed to search.php - it then redirects to the 
nice-looking bookmarkable GET address. This also avoids that nasty 
"would you like to resubmit the form" message if the user refreshes the 
page.
if ((!empty($_POST['search'])) && (!empty($_POST['keywords']))) {
    header('location: 
http://www.domain.com/search/'.cleanURL($_POST['keywords']).'/');
    exit;
}

if (preg_match('%^search/(.*)/?$%i', $_SERVER['REQUEST_URI'], $matches)) {
$keywords = $matches[1];
} else {
$keywords = '';
//display search form here perhaps
}
$keywords = urldecode(str_replace('+', ' ', $keywords));
$keywords_arr = explode(' ', $keywords);
//queries etc can start here

This function I use for creating the URL for the search page - note this 
is engineered for dashes instead of pluses as the separator....

function cleanURL($url)
    {
        /* Make url lower case */
        $url = strtolower($url);

        /* Convert non-UTF Characters */
        if (extension_loaded('mbstring') && 
mb_detect_encoding($url)!='UTF-8') $url = utf8_encode($url);

        /* Remove some characters */
        $matches = array( '"', '!', '#', '$', '%', '^', '*', '<', '>', 
'=', '\'', ',', '(', ')', '?', '.', '!', 
',','[',']','{','}',':',';','`','~','|');
        $url = str_replace($matches, '', $url);

        /* Replace some characters */
        $matches = array( '/', ' - ', ' ', ', ',   '&',  '@', ':', '--' );
        $replace = array( '-',   '-', '-',  '-', 'and', 'at', '-', '-'  );
        $url = str_replace($matches, $replace, $url);

        /* Remove remaining dashes */
        $url = str_replace('--','-',$url);
        $url = trim($url,'-');

       /* Encode to catch any remaining non-english characters  */
        $url = urlencode($url);

        return $url;
    }

Here's the search form - nothing particularly noteworthy here...

<form method="post" action="search.php">
<input type="text" name="keywords" value="" />
<input type="submit" name="search" value="Go" />
</form>

Also note it's generally not a bad idea to add a noindex/nofollow meta 
tag to search results pages, otherwise spiders can end up indexing 
search results pages instead of your good content.

You may want to look at fulltext indexing instead of a home-baked SQL 
query - depending on what you are searching through, it may give better 
results. Or look at running an 'exact phrase' query and listing those 
result first, then an 'all words' query and listing those results next, 
then doing an 'any words' query and listing those last. Writing a 
home-baked search feature for your site is not easy. The chief problem 
you have is that people are accustomed to an exceptionally high level of 
relevance from using Google/Bing/Whatever, so you will probably need to 
give some thought as to how you can make your search more relevant 
keeping in mind the things that make your data unique.
You should also look at ways of assigning more weight if the search 
terms show up in the title or tags fields of your data record (if the 
data has them).

This all can get very complex very quickly, but the reality is that 
visitors convert better when you give them good relevant search results.

Hope some of that helps.

Harvey.



matt_thomson wrote:
> Thanks Harvey,
>
> Any chance you can post a snippet of your code, I am planning to regex
> the request uri, get the search terms, and do a loop to write the sql.
>
> Most of the search scripts I have looked at use "WHERE column_value
> LIKE %".$search_term."%
>
> So my loop would end up making
>
> WHERE column_value_1 LIKE %".$search_term_1."% OR
> WHERE column_value_1 LIKE %".$search_term_2."% OR
> WHERE column_value_2 LIKE %".$search_term_1."% OR
> WHERE column_value_2 LIKE %".$search_term_2."% OR
>
> etc etc...
>
> Is this the kind of sql you are doing?
>
> On Jan 4, 12:29 pm, Harvey Kane <[email protected]> wrote:
>   
>> Personally, I prefer to read $_SERVER['REQUEST_URI'] and regex out the
>> bits I need. I don't think this is hacky at all - it's actually more
>> reliable across different server configs and gives you more control over
>> how your URLs appear. This is a good way to go especially once you
>> decide to rewrite your URLs aswww.domain.com/search/dog+cat+rabbit/-
>> you can ignore the querystring variables which makes your life easier
>> when writing your .htaccess rules.
>>
>> Once you have regexed 'cat+dog+rabbit' into a variable, do a str_replace
>> on the plusses to make them into spaces. Make sure you have handling
>> such as for other special characters on the way in and out of the URL
>> (ie when you create the search links and when you read them from the URL).
>>
>> Hope that helps a bit.
>>
>> Harvey.
>>
>>
>>
>>
>>
>> matt_thomson wrote:
>>     
>>> Hi, I have a script that runs like: search.php?terms=dog+cat+rabbit
>>>       
>>> echo $_GET['terms'] will output "dog cat rabbit" not "dog+cat+rabbit"
>>>       
>>> This is not desirable as I want to be able to tell the difference
>>> between the end of a search term, and a space within a search term. I
>>> also want to use the plus, as it is intuitive for users if they want
>>> to edit the url manually.
>>>       
>>> I have googled around and found a description of the why plus is
>>> replaced with a space here:
>>>       
>>> http://bugs.php.net/bug.php?id=39078
>>>       
>>> But there doesn't seem like there is any simple solutions. I am
>>> currently thinking it might be easiest to use $_SERVER["REQUEST_URI"]
>>> then regular expression it, but that seems like a bit of an ugly last
>>> resort way, does anyone have any better solutions.
>>>       
>>> Thanks,
>>>       
>>> Matt.
>>>       
>> --
>> Harvey Kane
>>
>> New Zealand:
>> -Office: +64 9 950 4133
>> -Mobile: +6421 811 951
>>
>> Email: [email protected]
>>  If you need to contact me urgently, please read my email 
>> policywww.ragepank.com/email/
>>     
>
>   

-- 
Harvey Kane

New Zealand:
-Office: +64 9 950 4133
-Mobile: +6421 811 951

Email: [email protected]
 If you need to contact me urgently, please read my email policy 
www.ragepank.com/email/

-- 
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]

Reply via email to