UPDATE: I made it better...now finds multiple words.
Code:
<?php
/*
* by chatmasta
* word_match($s1, $s2, $min_length = 4, $collapse_whitespace = true)
* compares two $s1 and $s2 and returns a shared substring of length greater
* than or equal to $min_length. if no shared substring is found, returns false.
* if $find_all is true, will find ALL words. otherwise will just find the first (longest) one.
* if $collapse_whitespace is true, deletes whitespace before all operations.
*/
function word_match($s1, $s2, $min_length = 4, $find_all = true, $collapse_whitespace = true) {
if($collapse_whitespace) {
$s1 = str_replace(' ', '', preg_replace('/\s\s+/', '', $s1));
$s2 = str_replace(' ', '', preg_replace('/\s\s+/', '', $s2));
}
$return = array();
$longer = (strcmp($s1, $s2) < 0) ? 's1' : 's2';
$shorter = (strcmp($s1, $s2) > 0) ? 's1' : 's2';
$st = similar_text($s1, $s2);
$remaining = strlen(${$longer}) - $st;
for($i = 0; $i < $remaining; $i++) {
$word_length = $st - $i;
$stop = ($find_all) ? strlen(${$shorter}) : strlen(${$shorter}) - $st + 1;
for($start = 0; $start < $stop; $start++) {
$check = substr(${$shorter}, $start, $word_length);
if(@strstr(${$longer}, $check) && strlen($check) >= $min_length) {
if($find_all) { $return[] = $check; }
else { return $check; }
}
}
}
return (count($return) > 0) ? array_unique($return) : false;
}
$s1 = 'blow up like the world trade';
$s2 = 'like trade';
if($match = word_match($s1, $s2)) { print_r($match); } else { echo 'fail'; }
?>