random words in sentence
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
random words in sentence
ok i have a string lets say it is 'he jumps high' ok then i have a sentence lets say it is 'when jumps high he killed the man that said that he jumps high' .
well what i need to do is find all occurences of the string in the sentence but the words of the string can be in any order in the sentence. so what im saying is i need to know that 'jumps high he' AND 'he jumps high' are in the sentence becuase it has the words in the string even though it isnt in the same order. how can i do this?
well what i need to do is find all occurences of the string in the sentence but the words of the string can be in any order in the sentence. so what im saying is i need to know that 'jumps high he' AND 'he jumps high' are in the sentence becuase it has the words in the string even though it isnt in the same order. how can i do this?
I think (as far as I know) you are going to have to make a function to stick all the possible combinations of words into an array, then go through the array with http://uk.php.net/stristr.
Although Feyd could probably make some regex thing that to me looks like someone holding down the shift key and randomly hitting numbers, but works just the way you want it to
Although Feyd could probably make some regex thing that to me looks like someone holding down the shift key and randomly hitting numbers, but works just the way you want it to
I'd probably do something like:
Code: Select all
$searchWords=explode(" ",$words);
$found=TRUE;
foreach($searchWords=>$aWord) {
if (strpos($stringToSearch,$aWord)===FALSE) $found=FALSE;
if ($found==FALSE) break;
}
if ($found) {
// handle match
} else {
// handle non-match
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
why not just seperate the words being searched for into seperate elements. Find the first occurance of any of those words, toss that word. Look at the next word, is it in the list of remaining words to search for? If so, toss that word, and look at the next, and so on, and so on...
Takes care of permutations without having to create them..
Takes care of permutations without having to create them..
Which, I think, is what my code above should do....feyd wrote:why not just seperate the words being searched for into seperate elements. Find the first occurance of any of those words, toss that word. Look at the next word, is it in the list of remaining words to search for? If so, toss that word, and look at the next, and so on, and so on...
Takes care of permutations without having to create them..
Ahh... they have to be adjacent, but order doesn't matter...
Hmm I think I'd hack up some sort of recusrive sort then:
in psuedo code:
explode the word list
loop through the word list. if you find a match, recurse using:
-- the word list minus the found word
-- the string to be searched starting after the found word
Hmm I think I'd hack up some sort of recusrive sort then:
in psuedo code:
explode the word list
loop through the word list. if you find a match, recurse using:
-- the word list minus the found word
-- the string to be searched starting after the found word