ruby_09 Posted October 16, 2009 Share Posted October 16, 2009 Hi , if I have $s1 = "XYZ12301"; $s2 = "XYZ12350"; I want to compare the two till they are similar and split them at the character where they are different so in this case split at XYZ123 01 XYZ123 50 i want 01 from s1 and 50 from s2 since "XYZ123" are common in both Link to comment https://forums.phpfreaks.com/topic/177938-how-to-compare-the-comman-characters-in-two-strings/ Share on other sites More sharing options...
cags Posted October 16, 2009 Share Posted October 16, 2009 Are the two strings always the same length? Link to comment https://forums.phpfreaks.com/topic/177938-how-to-compare-the-comman-characters-in-two-strings/#findComment-938172 Share on other sites More sharing options...
ruby_09 Posted October 16, 2009 Author Share Posted October 16, 2009 yes they are always of same length Link to comment https://forums.phpfreaks.com/topic/177938-how-to-compare-the-comman-characters-in-two-strings/#findComment-938173 Share on other sites More sharing options...
cags Posted October 16, 2009 Share Posted October 16, 2009 Lots of possibilities, heres one... $s1 = "XYZ12301"; $s2 = "XYZ12350"; $i = 0; while($s1[$i] == $s2[$i]) { ++$i; } $len = strlen($s1); $dif_1 = substr($s1, $i); $dif_2 = substr($s2, $i); Link to comment https://forums.phpfreaks.com/topic/177938-how-to-compare-the-comman-characters-in-two-strings/#findComment-938175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.