Jump to content

What would this be called?


justAnoob

Recommended Posts

I have a little search script. But I also want it to search for the plural of words also

 

ex:

guitar

guitars

 

or

crash

crashes

 

Basically adding either "s" and "es" to any word entered in the search box.

The question I got is, what is the name for adding letters to variables? I think I can figure it out if I know what to look for.

 

Link to comment
Share on other sites

I have a little search script. But I also want it to search for the plural of words also

 

ex:

guitar

guitars

 

or

crash

crashes

 

Basically adding either "s" and "es" to any word entered in the search box.

The question I got is, what is the name for adding letters to variables? I think I can figure it out if I know what to look for.

 

str replace google it

Link to comment
Share on other sites

I think so, I just need to create another variable off of the first one so my search script will search for the plural also.

I'll do some playing around. I think the only problem I may run into is if I already type the plural into the search box, then I need it to check for the singular tense of the word. Any input would be greatly accepted. But in the meantime, its coding time(more like trial and error for me, lol)

Link to comment
Share on other sites

it seems to be that you are going to try to make almost a database of plural form words,

 

would it not be better to use like?

 

eg in your query have LIKE 'crash%'

 

that would return crash, crashed, crashing, crashn (for those who like to abbreviate) ect

So my query,

<?php
$var = @$_GET['whatever'] ;
$trimmed = trim($var);
$query = "select * from MYTABLE where SOMETHING like \"%$trimmed%\"  or SOMETHING like \"%$trimmed%\"  order by ID";
?>

 

lets say I have the word "guitar" in my database, with this code, it will not return a result when i enter in "guitars" only if i enter "guitar". Didn't you say that it should return a result or is the query still wrong?

Link to comment
Share on other sites

Something like '%$trimmed%' will match to the word, and the word inside anything. so if you searched guitar, it would return guitars, bass guitar, electric guitars, etc. The % is a wildcard, and since you apply it to both sides, it means that anything could be next to the search term.

 

If you want just the word, and like plural variations, and stuff like that, '$trimmer%' might work. That would match a search of guitar with guitar, guitars. However, watch out for abnormal english words where the plural is different from the singular

Link to comment
Share on other sites

ooppss, double reply.

 

do you mean something like this.

<?php
$query = "select * from MYTABLE where SOMETHING like \"$trimmed%\"  or SOMETHING like \"$trimmed%\"  order by ID";
?>

 

Just take out the first %  ?

That only allows me to retrieve the exact word if it is the first word.

 

Link to comment
Share on other sites

ah i see what you are doing now,

 

the somethings would be replaced later by eg title and contents.

 

fair enough.

 

but as i said basically search things do not cater in general for searching guitars to try and find guitar, then simplest approach is to have a note to users informing them that searching for guitar will search for guitar, guitars, guitarist ect

 

may i suggest this as a tutorial for you as you seem to want to build your search up.

http://iamcal.com/publish/articles/php/search/

Link to comment
Share on other sites

I don't know what your data looks like, so I can't tell you what your query should be. If you want to match any columns with the word inside of it, then use '%$var%' if you just want to match different endings (IE match to the beginning of the word) use '$var%'. If you want to match the end of the word, use '%$var'

Link to comment
Share on other sites

I won't keep going on about this subject, just one last thing to mention.

 

Here is an example of a name and description in the database

 

Name: 1965 Gibson SG Electric Guitar

Description: rare 1965 gibson sg electric guitar. original owner. frets have pearl inlays. in very good condition with only minor scratches. plays great with no string buzz.

 

Now when I type in "guitar" in the search box, 65 Gibson is given to me as a result. (Nice!)

If I were to type in "guitars" in the search, i get no results (bad)

Also, if I were to type in "guita", the 65 Gibson is a result (still nice)

Link to comment
Share on other sites

thats because the words "guitars" isn't in the name or description.

 

You have a multitude of options. You can add a tags column to your DB, so you can put different tags in (like guitar, guitars, etc)

 

You could also trim s's from the end of strings. you could use rtrim() for this.

 

this might be a bad idea though, as taking of random characters from the end of strings could have unwanted results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.