Orionsbelter Posted September 4, 2011 Share Posted September 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246387-break-down-strings/ Share on other sites More sharing options...
gizmola Posted September 4, 2011 Share Posted September 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246387-break-down-strings/#findComment-1265249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.