Jump to content

[SOLVED] get the repeating words between 2 strings


jjk2

Recommended Posts

"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

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

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 :D

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.