sw45acp Posted June 14, 2009 Share Posted June 14, 2009 Hi! I am trying to write a function that will compare two or more words and return with the number of letters that happen to be in the same place. For example, melting and helping should return that their are 5 letters in the same place, or 5/7. The function that I have written could probably be written more efficiently. Any help would be appreciated! For some reason this returns zero instead of 5. Basically what I am trying to design is a guess game. Where you have a list of words and in that list is the correct word. You need to find it by guessing. Choosing a word will tell you how many of the characters are in the correct positions. function compare($s1,$s2) { $total = 0; if ($s1{0} == $s2{0}){ $total + 1; } if ($s1{1} == $s2{1}){ $total + 1; } if ($s1{2} == $s2{2}){ $total + 1; } if ($s1{3} == $s2{3}){ $total + 1; } if ($s1{4} == $s2{4}){ $total + 1; } if ($s1{5} == $s2{5}){ $total + 1; } if ($s1{6} == $s2{6}){ $total + 1; } echo $total; } compare("melting","helping"); Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/ Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 function compareWords($word, $guess, $caseSensitive = false) { if ($caseSensitive == false) { $word = strtolower($word); $guess = strtolower($guess); } $c = strlen($word); for ($pos = 0;$pos < $c;$pos++) { if ($word{$pos} === $guess{$pos}) $num++; } return $num; } // end compareWords example use: echo compareWords('something','something'); // output: 9 echo compareWords('Something','something',true); // output: 8 echo compareWords('something','gomthenig'); // output: 3 Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855358 Share on other sites More sharing options...
sw45acp Posted June 14, 2009 Author Share Posted June 14, 2009 Thank you! That is excellent. I will work with this for a while. Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855377 Share on other sites More sharing options...
sw45acp Posted June 14, 2009 Author Share Posted June 14, 2009 I put together a temporary form and it works perfectly. However, I would like help on building the other side of the game, such as a tool to kind of hack it. I would like a user to be able to input a list of words, one of them being the unknown answer. This is like a hack or a cheat for it. So if someone tested a word, it tells them the number of characters correct, and then suggests the next best answer. Here is an example, like from the fallout 3 video game. http://perl.hacker.dk/cgi-bin/fallout_hack.pl Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855449 Share on other sites More sharing options...
Mark Baker Posted June 14, 2009 Share Posted June 14, 2009 Doesn't PHP's existing similar_text() function do this already? Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855500 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 Doesn't PHP's existing similar_text() function do this already? No. And neither does levenshtein. What those 2 functions do (in their own ways) is calculate the shortest amount of "moves" or "changes" it takes to get from one string to another. So for instance, if I compared "something" vs. "somethings" : it would take one "move" to make them equal: remove the "s" "something" vs. "osmething" : would return 2 etc.. which mostly seems like the opposite of what he wants, but it is not always the opposite, so you can't even exploit the oppositeness. Not really going to get into the why's of that, as frankly, I do not fully understand the those algorithms, just that in short, as mentioned, they try to calculate the shortest amount of changes it would take to make string1 into string2. Now, if the OP would like to take a different approach to his game or whatever, those 2 functions might indeed be something of interest to him, but again, they can't really be used for what he's asking. Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855574 Share on other sites More sharing options...
sw45acp Posted June 14, 2009 Author Share Posted June 14, 2009 Ok well thank you guys for trying. I bet it can be done somehow, but at least I have the start of the game. Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855603 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 okay well hold up, as far as suggesting the next best answer, something like levenshtein() should do the trick for you. For some reason in my last post I was still thinking about your OP. similar_text() and levenshtein() are indeed useful for figuring out a "next best answer" sort of thing. Why? Because they return how many "steps" it takes to get from one string to another, so you can loop through each wrong answer, comparing them to the right answer, and the smaller the number returned, the "closer" that string is to the answer. Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855607 Share on other sites More sharing options...
sw45acp Posted June 14, 2009 Author Share Posted June 14, 2009 ok i will take a look at those. the similar_text one sounds good because it can return a percent of similarity and that easily be rounded. thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855711 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 well levenshtein() is a lot more efficient at doing the same thing, and translating the result into a % is trivial.. I suppose if you're just comparing single words you aren't going to even come close to noticing the difference in efficiency, so sure, go for the similar_text and skip the extra step of translating it into % Quote Link to comment https://forums.phpfreaks.com/topic/162097-solved-comparing-characters-possibly-an-algorithm/#findComment-855718 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.