jjk2 Posted May 2, 2009 Share Posted May 2, 2009 "mary had a little lamb which was not fat" "bloody mary had a little freaking lamb that was fat." how do i get same repeating words? the first string is subset of the second. so i should just get: mary, had, a, little, lamb, was, fat Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 $str1 = 'mary had a little lamb which was not fat'; $str2 = 'bloody mary had a little freaking lamb that was fat'; $words = explode(' ', $str1); $matches = array(); foreach ($words as $word) { if (strpos($str2, $word) !== false) $matches[] = $word; } print_r($matches); Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/#findComment-824037 Share on other sites More sharing options...
jjk2 Posted May 2, 2009 Author Share Posted May 2, 2009 is there an easier function that just does this ? Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/#findComment-824055 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Not that I can think of right now. Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/#findComment-824063 Share on other sites More sharing options...
jjk2 Posted May 2, 2009 Author Share Posted May 2, 2009 now what if i want to compare multiple strings and count the number of times a keyword repeats? Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/#findComment-824079 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 Then you would have to extend Ken's code: <?php $strings = array('there is something about mary..', 'some more mary', 'and a little more.. mary'); $words = explode(' ', array_shift($strings)); $matches = array(); foreach ($words as $word) { foreach ($strings as $string) { if (strpos($string, $word) !== false) $matches[] = $word; } } ?> No problem to big, no problem to small for us guru's to solve ain't it Ken Quote Link to comment https://forums.phpfreaks.com/topic/156488-solved-get-the-repeating-words-between-2-strings/#findComment-824094 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.