so I have this idea, a search phrase like "candy apple red" against another string of equal or more words (if lesser, we do the search comparision backwards).
So, I put my finger on the word candy in candy apple red on the left side, and then start going through my comparison string:
"I want an apple and my red candy is hot",
let's say. I hit I, want, an, apple, .. , then CANDY - so I found one word. I now move my finger to APPLE on the left, and "is" on the right, then move to hot - now, I "flip" or roll back to the first entry on the right - I've had one flip. now I put my finger on I, want, an, APPLE - so I found two words. I then move to RED on the left, and on the right move my finger to and, my, RED ..
so we found 3 words and 1 flip. The more flips (rollovers), the more out of order we are, and the more words we find the better the match. You geniuses can do what you want with it - long as you share it with me but is my code flawed in any way.
The parameter that would be good to add is closeness or proximity - less number of words between matches, the tighter the cluster and we can evaluate that too.
Appreciate your review of my code!
echo '<pre>';
function search_compare_strings($needle, $haystack, $mode=''){
if(!$mode /* early version */){
return (strtolower($needle)==strtolower($haystack) ? 1 : 0);
}else if($mode=='tight keywords'){
/*
"tight" means one space between, small length
*/
//we are not looking for repeats, just a percentage match
if(!$needle || !$haystack) return 0;
if($needle==$haystack) return 1;
if(strtolower($needle)==strtolower($haystack))return .95;
$needle=explode(' ',$needle);
$haystack=explode(' ',$haystack);
$sub=(count($haystack)>count($needle) ? $needle : $haystack);
$super=($sub==$needle ? $haystack : $needle);
print_r($sub);
print_r($super);
$supercount=count($super);
while(list(,$a) = each($sub)){
echo 'loop level 1<br>';
echo 'each word[1] = '.$a . '<br>';
while(list(,$b) = each($super)){
$j++;
echo 'loop level 2, super = '.$b . '<br>';
if(isset($lastIdx) && $j==$lastIdx){
//couldn't find the word, move to next word
echo 'cannot find the word<br>';
if(!(list(,$a)=each($sub))) break; //get the next word from sub
echo 'each word[2] = '.$a . '<br>';
}
//case mismatch not currently considered
if(strtolower($a)==strtolower($b)){
$wordMatch++;
echo 'found the word '.$a . '<br>';
$lastIdx=$j;
//get the next word
echo '.<br>';
if(!(list(,$a)=each($sub))) break;
echo 'each word[3] = '.$a . '<br>';
}
//continue to roll around the super list
if(current($super)===false){
echo 'resetting<br>';
$j=0;
$flips++;
reset($super);
}
}
}
//stats
echo 'flips: ' . $flips.'<br />';
echo 'words: ' . $wordMatch.'<br />';
}
}
$a='istocome was cool is';
$b='I am he who is and was and istocome';
search_compare_strings($a,$b,'tight keywords');