0perator Posted June 18, 2008 Share Posted June 18, 2008 if i have 2 strings $value_sc // scrambled value $value_unsc //unscrambled value if $value_sc = "qewryt" and $value_unsc = "qwerty" how do i check both the strings to see if they contain the same characters? Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/ Share on other sites More sharing options...
Jabop Posted June 18, 2008 Share Posted June 18, 2008 explode them and then asort() them, check compare array values? My best guess Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568413 Share on other sites More sharing options...
Orio Posted June 18, 2008 Share Posted June 18, 2008 <?php function is_scrambled ($scrambled, $unscrambled) { if(strlen($scrambled) != strlen($unscrambled)) return false; $letters = str_split($scrambled); $num_letters = count($letters); sort($letters); for($i = 0; $i < $num_letters; $i++) if($letters[$i] != $unscrambled[$i]) return false; return true; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568422 Share on other sites More sharing options...
kenrbnsn Posted June 18, 2008 Share Posted June 18, 2008 Here's another way: <?php $value_sc = "qewryt"; $value_unsc = "qwerty"; $sc_arr = str_split($value_sc); $unsc_arr = str_split($value_unsc); sort($sc_arr); sort($unsc_arr); $diff = array_diff($sc_arr,$unsc_arr); if (empty($diff)) echo 'The two strings have the same characters'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568428 Share on other sites More sharing options...
Barand Posted June 18, 2008 Share Posted June 18, 2008 After the sorts you can just compare the arrays ... if ($sc_arr == $unsc_arr) echo 'The two strings have the same characters'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568436 Share on other sites More sharing options...
Orio Posted June 18, 2008 Share Posted June 18, 2008 After the sorts you can just compare the arrays I wasn't too sure how fast str_split() is, so I thought using str_split() only once and then looping over the characters would be faster. But I guess that should be tested. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568470 Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 I would expect Orio's to be a tiny bit faster, because when you compare an array, how exactly does PHP go about doing that? Does it check each element? Also, you have to sort with the second one. Also, Orio's would probably scale better, since operating on arrays logically becomes slower as the array gets bigger (and the rate at which a loop slows, I would assume would be slower). Anyway, just a guess. Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568483 Share on other sites More sharing options...
Barand Posted June 18, 2008 Share Posted June 18, 2008 the main problem with Orio's is that it only works if the unscrambled string has its letters already sorted echo is_scrambled('yterwq','qwerty') ? 'yes' : 'no' ; // --> no Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568490 Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Oh you're right.... Didn't see that.... I didn't read his entire code, just looked at the main difference between the two. The only way that Orio's would work would be if the unscrambled string was already sorted.... Guess Orio needs another str_split and sort after all x.x. Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568495 Share on other sites More sharing options...
Jabop Posted June 18, 2008 Share Posted June 18, 2008 kenrbnsn's method looks like it works just fine, I haven't tested it, but it's simple. I don't think the benching would really matter on such a small function Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568498 Share on other sites More sharing options...
0perator Posted June 19, 2008 Author Share Posted June 19, 2008 thanks loads i used kenrbnsn method, seemed easiest for what i was doing other places in the script. cheers boys Quote Link to comment https://forums.phpfreaks.com/topic/110785-stringy-thingies/#findComment-568898 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.