Jump to content

Break Down Strings


Orionsbelter

Recommended Posts

How can i break down strings for example i have a simple search database for products where as a user searches a term this can be one, two three words and so on.

 

I'm using a wildcard; but i want the best way to get the possible search so i would like to break down the string into separate words and then use that to search the database using wildcards

 

is this possible?

 

Thanks for reading. 

Link to comment
Share on other sites

Yes it's certainly possible.  Take a look at explode as an example of a helpful function.  I personally do not advise people to do sql queries with LIKE '%someword%'.  Any like query with '%....' is unable to use an index, which means that in order to execute your search it needs to look through every single row in the table and check for a match.  As your table gets larger and larger, the search will get slower and more resource intensive.  If however, the table is not that large, and can be kept in memory most of the time, it may be alright for your site.

 

There are alternatives -- mysql has fulltext search which can be used, and many people use alternative document indexing like the sphinx engine.  It is also possible to build your own efficient search table, that breaks up a sentence into a list of all relevant words (you want to exclude punctuation, common nouns and conjunctions, etc.) and store these in a "keywords" table.  You make a many to many resolver table between this table and the original table, and you'll have an efficient solution to searching for any number of words that doesn't require the use of LIKE '%word%".

 

I mention these options so you can take a look at them, but the plain and simple answer is that you can explode the search string into an array (although again you want to strip out meaningless words) and create a like query with:  LIKE '%word1%' OR '%word2' OR... etc

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.