gretorx Posted May 14, 2010 Share Posted May 14, 2010 I've been working on a piece of code, but can't seem to wrap my head around it. It is supposed to perform the following steps: 1) Convert each character of user input to ascii value (eg. a = 97) 2) Adds the user input characters together (eg. aaa = 97 + 97 + 97, which equals 291) 3) Sets that value as a variable 4) Opens a text file containing a list of words (eg. list.txt in this case) 5) Parses the contents of the text file into an array (eg. using the explode function, separating the words in list.txt by commas). This should make each word a separate item in the array. 6) Converts each character of each parsed word into ascii value (eg. just like step one) 7) Adds the values of the characters together (eg. just like step two) And finally, returns the result of which word in the text file has the same value of the user input. This serves as basically a dictionary unscrambler. The code I have is as follows <?php $input=$_GET['string']; $len=strlen($input); $file="list.txt"; $contents=fopen($file, "r"); $read=fread($contents, filesize($file)); $parse = explode(', ', $read); for ($j=0;$j<$len;$j++){ $char[$j]=ord($input[$j]); $compare=array_sum($char); } for ($i=0;$i<filesize($file);$i++){ for ($k=0;$k>len($parse2);$k++){ $parse2=$parse[$i]; $char[$k]=ord($parse2[$k]); $compare2=array_sum($char); } } if ($compare2=$compare1){ echo $input . "Contains the same ascii value as" . $parse2; ] ?> <html> <body> <form method="get" action="SOP3.php"> <input type="text" name="string" value=""> <input type="submit" value="Submit"> </form> </body> </html> I'm new to php if you can't tell, so I'm sure there are some simple mistakes in the code. Any help you could offer would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/ Share on other sites More sharing options...
andrewgauger Posted May 14, 2010 Share Posted May 14, 2010 Instead of adding them together you would need to multiply each letter by 255 to the power of their position. 97*256^2 97*256^1 +97*256^0 ----------------- 97+24832+6356992 = 6381921 Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058419 Share on other sites More sharing options...
gretorx Posted May 14, 2010 Author Share Posted May 14, 2010 Adding them together words fine, as it returns the proper output; however, I'm not sure if the code above is comparing the user input with the text file data. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058421 Share on other sites More sharing options...
gwolgamott Posted May 14, 2010 Share Posted May 14, 2010 First off isn't there a logic problem to this? You could end up with different words using the same ascii sum value... for example 9+2+3 sum is the same as the sum of 2+12.... Neither here nor there though.... I'd use a str_split() function on the word after you've got the word. Thus do a foreach to go through your arrays instead Once you have your word I'd then split the word into separate strings with str_split() <?php $input=$_GET['string']; $len=strlen($input); $file="list.txt"; $contents=fopen($file, "r"); $read=fread($contents, filesize($file)); $parse = explode(', ', $read); foreach ($parse as $value) { $String_split = str_split($value, 1); $Temp = 0; //resets itself before each word here foreach($String_split as $value_2) { $Temp = $Temp + ord($value_2); // adds each ascii value } if($Temp == $input) { echo $input . "Contains the same ascii value as" . $value_2; break; } else{$Temp = "NONE";} } if($Temp == "NONE"){ echo "No Match Found.";} Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058436 Share on other sites More sharing options...
gretorx Posted May 14, 2010 Author Share Posted May 14, 2010 Thank you very much for your help... by checking out the foreach php function and using your code as a reference, I was able to do exactly what i wanted to do. What a learning experience. I really appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058483 Share on other sites More sharing options...
gwolgamott Posted May 14, 2010 Share Posted May 14, 2010 No problem man, as long as it was helpful. I did forget to mention I hadn't tested it... so hopefully it wasn't too much an issue to modify it. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058494 Share on other sites More sharing options...
gretorx Posted May 14, 2010 Author Share Posted May 14, 2010 Nope, didn't take long at all. New question though. I've been trying to load the user input as an array, but only the first word gets unscrambled. As you can see from the code, I've attempted to parse the user input the same way that the text file input is being parsed; however, after the code finds matches for the first word, it just stops. Here is what I have: <?php //Reads the contents of the user input $input = $_GET['string']; $len = strlen($input); //Reads the contents of the dictionary file and writes it to a variable $file = "list.txt"; $contents = fopen($file, "r"); $read = fread($contents, filesize($file)); //Separates each word after a ',' and puts it into the $parse[] array $parse = explode(', ', $read); $parse2 = explode(', ', $input); //Converts each character in the user input into numerical value, then multiplies the numbers together. foreach ($parse2 as $val) { $split=str_split($val, 1); $compare = 0; foreach ($split as $val2) { $compare=$compare+ord($val2); } } foreach ($parse as $value) { $split2=str_split($value, 1); $compare2 = 0; foreach ($split2 as $value2) { $compare2=$compare2+ord($value2); if($compare2 == $compare) { echo $value . " Contains the same ascii value as " . $val . "<br><br>"; break; }else{ $compare2 = "none"; } } } if($compare == "none") { echo "The value of " . $val . " is " . $compare . ", and does not have any matches<br><br>"; } ?> <html> <body> <form method="get" action="SOP3c.php"> <input type="text" name="string" value=""> <input type="submit" value="Submit"> </form> </body> </html> Does anyone have any articles I can read on this? I'm all about learning, that's why I'm posting. Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058526 Share on other sites More sharing options...
andrewgauger Posted May 15, 2010 Share Posted May 15, 2010 Try posting the form and reading the $_POST variable. Get doesn't work well with spaces. <form method="post" action="script.php"> $input = $_POST['string']; foreach ($parse2 as $val) { $split=str_split($val, 1); $compare = 0; foreach ($split as $val2) { $compare=$compare+ord($val2); } foreach ($parse as $value) { $split2=str_split($value, 1); $compare2 = 0; foreach ($split2 as $value2) { $compare2=$compare2+ord($value2); if($compare2 == $compare) { echo $value . " Contains the same ascii value as " . $val . "<br><br>"; break; }else{ $compare2 = "none"; } } } if($compare == "none") { echo "The value of " . $val . " is " . $compare . ", and does not have any matches<br><br>"; } } I moved a semi-colon so that your 2 major foreach(s) would be nested. *untested* Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058591 Share on other sites More sharing options...
gretorx Posted May 15, 2010 Author Share Posted May 15, 2010 From what I can tell, it doesn't change anything... the last user input array item is the only item being compared. :S Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058637 Share on other sites More sharing options...
ignace Posted May 15, 2010 Share Posted May 15, 2010 $w1 = 'hello'; $w2 = 'oelhl'; $p1 = array_sum(array_map('ord', str_split($w1))); $p2 = array_sum(array_map('ord', str_split($w2))); if ($p1 === $p2) { echo 'equal'; } else { echo 'not equal'; } //Output: equal function str2int($word) { return array_sum(array_map('ord', str_split($word))); } function word_is_like($word, $like) { return str2int($word) === str2int($like); } $match = $_GET['match']; $words = file('path/to/text/file.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); foreach ($words as $word) { if (word_is_like($word, $match)) { echo $word, ' equals ', $match, "<br>\n"; } } [ot]Tell your teacher to come up with something more difficult[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058641 Share on other sites More sharing options...
andrewgauger Posted May 15, 2010 Share Posted May 15, 2010 I usually have a difficult time understanding when to use 3 equal signs. Why is it important here? Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058810 Share on other sites More sharing options...
ignace Posted May 15, 2010 Share Posted May 15, 2010 I usually have a difficult time understanding when to use 3 equal signs. Why is it important here? It isn't. I got in to an habit of using them to notify readers that I'm comparing two variables of the same type. I use == whenever I compare to an unknown source that may be or may not be a numeric string (in the latter it will return 0). Syntactical sugar == equal === identical Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058819 Share on other sites More sharing options...
andrewgauger Posted May 15, 2010 Share Posted May 15, 2010 Yeah, I just figured that unless one was null they would be identical in this example. Syntactical sugar awesome. So it would be good for 0 == null <-true 0 === null <-false ? Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058824 Share on other sites More sharing options...
ignace Posted May 15, 2010 Share Posted May 15, 2010 Yeah, I just figured that unless one was null they would be identical in this example. Syntactical sugar awesome. So it would be good for 0 == null <-true 0 === null <-false ? Yeah it all started because I didn't like 1 == true|1|'1' Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058830 Share on other sites More sharing options...
andrewgauger Posted May 15, 2010 Share Posted May 15, 2010 1 == true|1|'1' do you mean 1===true|1|'1' Shouldn't 1==true evaluate to true? Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058862 Share on other sites More sharing options...
ignace Posted May 15, 2010 Share Posted May 15, 2010 1 == true|1|'1' do you mean 1===true|1|'1' Shouldn't 1==true evaluate to true? No I really meant 1 == true|1|'1' which all evaluate to true. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058873 Share on other sites More sharing options...
salathe Posted May 15, 2010 Share Posted May 15, 2010 First off isn't there a logic problem to this? You could end up with different words using the same ascii sum value... Hmm; iguanodon, mongolian, pakistani, wednesday, lightning, magnetize, nostalgic, pistachio, millimole, scrapbook and unsayable (to name a few examples) all have the same ASCII number total (964) as phpfreaks. Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058894 Share on other sites More sharing options...
andrewgauger Posted May 16, 2010 Share Posted May 16, 2010 So it looks like ASCII value alone is not enough to unscramble. You will have to verify that the length of the two words are the same as well. strlen($word)==strlen($like) Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1058963 Share on other sites More sharing options...
salathe Posted May 17, 2010 Share Posted May 17, 2010 Really? strlen('pistachio')==strlen('phpfreaks')\ Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1059396 Share on other sites More sharing options...
gwolgamott Posted May 17, 2010 Share Posted May 17, 2010 Really? strlen('pistachio')==strlen('phpfreaks')\ Gotta use some psychic powers... Quote Link to comment https://forums.phpfreaks.com/topic/201777-comparing-two-strings-same-letters-but-different-letter-positions/#findComment-1059481 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.