Topic: Search for list of words in a string (Read 567 times)
Global Moderator
Internet Junkie
Gender:
Posts: 1523
6847 credits Members referred : 8
Gimme all your cookies!!!
« on: Dec 05, 2005, 09:11:15 AM »
I have a list of words and their definitions in a db table. Now I want to check to see if any of those words appear in a string, probably only a paragraph long.
What is the best way to search through the string for those words? I guess putting the words into an array and then checking if any one of those words are in the string. I just want to find the fastest method and the one that is the least cpu intensive.
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8250
42487 credits Members referred : 3
« Reply #1 on: Dec 05, 2005, 12:03:01 PM »
That propably depends on how many words you have in your paragraphs.
If you are going to do something like a thesaurus where people get the mouse over a word and see the meaning of the word, let me tell you the logic I used in a simmilar script. Of course this logic is suitable only if you have lots of words in your paragraphs, and in your db.
First I take the words in the paragraph, and split them by spaces. Then I create a query that search only for those words. Then from the db result, I do something like this :
Code:
<?php foreach ( $words as word => $meaning ) { $paragraph = str_replace ( $word, $meaning, $paragraph ); } ?>