Jump to content

[SOLVED] get the repeating words between 2 strings


jjk2

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.