Anti-Moronic Posted May 25, 2009 Share Posted May 25, 2009 Is there a way to compare array values? Like: array("i am a string", "i am a zebra"); Would there be a way to compare both of them and give me a percentage on the comparison? I have no idea how to approach this. Any help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 25, 2009 Share Posted May 25, 2009 levenshtein might be useful for you. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Author Share Posted May 25, 2009 Wow, thanks...I searched for ages, couldn't come up with anything. Function looks extremely interesting. Thank you! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted May 25, 2009 Share Posted May 25, 2009 this can be intensive especially on large arrays... the functions to compare strings you will be interested are: http://uk3.php.net/manual/en/function.levenshtein.php and http://uk3.php.net/manual/en/function.similar-text.php if you needed to compare each string against every other string in your array then you would need to build the appropriate loop; something like <?php $strings = array("i am a string", "i am a zebra","i am not a string","i am a wildebeast","i am a thread","i am a matrix"); $string_no = count($strings); $compare = array(); for($i=0; $i<$string_no; $i++) { if ($i == ($string_no-1)) break; for($k=$i+1; $k<$string_no;$k++) { $compare['strings'][] = $i . ' - ' . $k; $compare['likeness'][] = levenshtein($strings[$i],$strings[$k]); } } print_r($compare); ?> Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 25, 2009 Author Share Posted May 25, 2009 Cheers Toon, you just saved me even more time and confusion. Much appreciated. Quote Link to comment 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.