AP81 Posted June 2, 2008 Share Posted June 2, 2008 Hi, I needed to compare strings to see if they contain all the same characters, but aren't identical. For example, comparing 2345 and 3245. These strings aren't identical, but contain the same characters. The reason why I needed to do this was to cross check some numbers from two different databases (a couple of previous employees were dyslexic). I am porting over all this information from one DB to another, thus I am cleaning the data to make sure it is accurate. Anyway, I've written my own solution to this (see strings_contain_same_chars function below) as I couldn't find a PHP function which does this. Although it works fine, there is a fair amount of overhead by creating arrays to store the data, then sort, then implode. I was wondering if there is a neater way to do this. <?php $string1 = '2345'; $string2 = '3245'; function strings_contain_same_chars($string1, $string2) { $lenStr1 = strlen($string1); $lenStr2 = strlen($string2); if ($lenStr1 != $lenStr2) { // invalid if not the same length return false; } else { for ($i=0; $i<$lenStr1; $i++) { $str1[$i] = substr($string1, $i, 1); } for ($i=0; $i<$lenStr2; $i++) { $str2[$i] = substr($string2, $i, 1); } sort($str1); sort($str2); $str1 = implode('', $str1); $str2 = implode('', $str2); if ($str1 == $str2) { return true; } return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108331-string-compare-can-u-improve-my-code/ Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Well, you could have made your code a lot shorted by converting the strings to arrays, sorting and then checking for equality. Something like: <?php $string1 = 'abc'; $string2 = 'acb'; function strings_contain_same_chars($string1, $string2) { $array1 = str_split($string1); $array2 = str_split($string2); sort($array1); sort($array2); if($array1 == $array2){ return true; }else{ return false; } } if(strings_contain_same_chars($string1,$string2)){ echo 'true'; }else{ echo 'false'; } ?> Wether or not it's any more efficient, im not sure. Quote Link to comment https://forums.phpfreaks.com/topic/108331-string-compare-can-u-improve-my-code/#findComment-555392 Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Did some benchmarking -- my method is about twice as quick: <?php $string1 = 'abcdefghi'; $string2 = 'dbceghifa'; function function1($string1, $string2) { $array1 = str_split($string1); $array2 = str_split($string2); sort($array1); sort($array2); if($array1 == $array2){ return true; }else{ return false; } } function function2($string1, $string2) { $lenStr1 = strlen($string1); $lenStr2 = strlen($string2); if ($lenStr1 != $lenStr2) { // invalid if not the same length return false; } else { for ($i=0; $i<$lenStr1; $i++) { $str1[$i] = substr($string1, $i, 1); } for ($i=0; $i<$lenStr2; $i++) { $str2[$i] = substr($string2, $i, 1); } sort($str1); sort($str2); $str1 = implode('', $str1); $str2 = implode('', $str2); if ($str1 == $str2) { return true; } return false; } } $t1 = microtime(true); for($x=0;$x<=10000;$x++){ function1($string1,$string2); } $t2 = microtime(true); for($x=0;$x<=10000;$x++){ function2($string1,$string2); } $t3 = microtime(true);//end time //now output results printf ("1st Method: %0.8f<br>2nd Method: %0.8f<br>Ratio (1st method/2nd method): %0.2f", $t2-$t1, $t3-$t2, ($t2-$t1)/($t3-$t2)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108331-string-compare-can-u-improve-my-code/#findComment-555394 Share on other sites More sharing options...
AP81 Posted June 2, 2008 Author Share Posted June 2, 2008 Sweet, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/108331-string-compare-can-u-improve-my-code/#findComment-555398 Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Or even quicker: function function2($string1, $string2) { $array1 = str_split($string1); $array2 = str_split($string2); $count1 = count($array1); $count2 = count($array2); if(count(array_intersect($array1,$array2))==$count1 &&$count1==$count2){ return true; }else{ return false; } } Interestingly, the speed difference is more noticeable with a set of numbers than a set of letters. Not sure why that should be, though. <?php $string1 = 'abcdefghi'; $string2 = 'ihgfedcba'; $string1 = '1234567'; $string2 = '7654321'; function function1($string1, $string2) { $array1 = str_split($string1); $array2 = str_split($string2); sort($array1); sort($array2); if($array1 == $array2){ return true; }else{ return false; } } function function2($string1, $string2) { $array1 = str_split($string1); $array2 = str_split($string2); $count1 = count($array1); $count2 = count($array2); if(count(array_intersect($array1,$array2))==$count1 &&$count1==$count2){ return true; }else{ return false; } } $t1 = microtime(true); for($x=0;$x<=10000;$x++){ function1($string1,$string2); } $t2 = microtime(true); for($x=0;$x<=10000;$x++){ function2($string1,$string2); } $t3 = microtime(true);//end time //now output results printf ("1st Method: %0.8f<br>2nd Method: %0.8f<br>Ratio (1st method/2nd method): %0.2f", $t2-$t1, $t3-$t2, ($t2-$t1)/($t3-$t2)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108331-string-compare-can-u-improve-my-code/#findComment-555403 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.