xiaix Posted October 20, 2011 Share Posted October 20, 2011 Best method to for the following? // The following $str1 and $str2 should say they are identical $str1 = "abcDEF"; $str2 = "EcFabD"; // The following $str1 and $str2 should say they are NOT identical because of the "a" "A" case difference. $str1 = "abcDEF"; $str2 = "EcFAbD"; I'm comparing bitvector strings. "ABC" is different than "abc". "abc" is the same as "cba". What's the best php function or method I should use? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/249493-compare-chars-in-a-string/ Share on other sites More sharing options...
creata.physics Posted October 20, 2011 Share Posted October 20, 2011 Try to google 'php string comparison'. You'll find what you need, you can take a look at php's function: http://www.php.net/manual/en/function.similar-text.php It returns the number of matching chars in both strings, which isn't exactly what you want, but I'm sure there is something close enough around there. As far as I know, there is no function in php that will compare two different strings and tell you what ones share the same chars and what are different. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/249493-compare-chars-in-a-string/#findComment-1280970 Share on other sites More sharing options...
benphp Posted October 20, 2011 Share Posted October 20, 2011 strToLower? $str1 = strtolower($str1); $str2 = strtolower($str2); if ($str1 == $str2) { print "we're the same"; } Quote Link to comment https://forums.phpfreaks.com/topic/249493-compare-chars-in-a-string/#findComment-1280976 Share on other sites More sharing options...
kicken Posted October 20, 2011 Share Posted October 20, 2011 Use count_chars and array_diff <?php $a = 'abcDEF'; $b = 'EcFabD'; $aChars=count_chars($a, 1); $bChars=count_chars($b, 1); if ($aChars==$bChars){ echo "Strings match"; } else { echo "No match"; } [edit] No need for the array stuff aparently Quote Link to comment https://forums.phpfreaks.com/topic/249493-compare-chars-in-a-string/#findComment-1280980 Share on other sites More sharing options...
xiaix Posted October 20, 2011 Author Share Posted October 20, 2011 Use count_chars and array_diff <?php $a = 'abcDEF'; $b = 'EcFabD'; $aChars=count_chars($a, 1); $bChars=count_chars($b, 1); if ($aChars==$bChars){ echo "Strings match"; } else { echo "No match"; } [edit] No need for the array stuff aparently Yep, that works great. It's better than what I was starting to do... $a = "abcDEF"; $b = "abcDEF"; $lenA = strlen($a); $lenB = strlen($b); if ($lenA != $lenB) { echo "Diff Len, no need to continue."; exit(); } $data1 = preg_split("//", $a); $data2 = preg_split("//", $b); $len = $lenA; $found = 0; for ($x = 1; $x <= $len; $x++) { $found = 0; for ($y = 1; $y <= $len; $y++) { if ($data1[$x] == $data1[$y]) { $found = 1; break; } } if ($found == 0) break; } if ($found == 0) echo "No match"; Which wasn't quite working anyway. Quote Link to comment https://forums.phpfreaks.com/topic/249493-compare-chars-in-a-string/#findComment-1280983 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.