Removing +'s from search string

greenspun.com : LUSENET : S-Mart Shopping Cart : One Thread

Here's one problem that I haven't been able to solve yet - Maybe someone else can point me in the right direction to look towards an answer.

When people search my online store sometimes they enter + before the term they are searching for: Ie: if they wanted to search for BIG BOX they might enter +BIG +BOX thinking that this search script works just like the commercial search engines.

Of course, with S-Mart, no results are returned since it counts the +'s in the string that it is looking for which it will never find.

Here is my question, anyone have any ideas of a way to make the script take the inputted text and remove any +'s or other stray characters ("'s, -'s, etc) that the user might have put into the search box?

Thanks everyone for your input and suggestions as to how this might be accomplished!

Please note: This message is posted to both S-mart forumns. Sorry if you get two copies of it but I want to make sure that everyone sees it.

-- BP (bppilot@aol.com), September 17, 1999

Answers

I don't use S-Mart as my search engine to search my database, so I can't point you to the specific spot in the code where the search is carried out (didn't find it with a quick glance through the script). But in general, you only need to add one line of code.

Immediately after your search variables are parsed, and before they are passed to the actual search routine, add a line that looks like this (where $input_variable is whatever variable you want to strip the "+" sign out of):

$input_variable =~s/\+//g;

Key elements - the "\" before the "+" is necessary because the + is a Meta character with a special meaning unless negated by the \.

the "g" at the end means to replace all copies of "+" with what's between the last two //. Of course that's empty, so it just removes the +'s and replaces with an empty string.

The question of what is a "stray" character and what is a legitimate search character will have to be defined by you ahead of time. If you want to define several characters at once as "stray", you can use the following

$input_variable=~tr/+-*@/ /;

this will translate any occurrence of the characters between the first two // into the correspondingly positioned character between the second two //. Since these are all blanks, each one will be turned into a space. I don't know if this will work with an empty string between the second two //. You'll have to test.

That should do it.

Please post back if it worked so the next person learns something too.

Roy

-- Roy Lingen (rlingen@speedline.ca), September 18, 1999.


Roy -

Thanks for contributing an answer to this question. I implemented it into the S-Mart script so that now any time that sometimes types in something like +BIG +BOX it will change it to just BIG BOX.

Here is where I put the code you gave me with a minor modification to fit this particular application:

Right after the sub gen_page section starts, add:

$FORM{'search'}=~s/\+\\g;

That's it. Thanks again for your contribution and help with this Roy!

-BP

-- BP (bppilot@aol.com), September 24, 1999.


Moderation questions? read the FAQ