cunoodle2 Posted May 14, 2009 Share Posted May 14, 2009 Can I compare two strings and return a value somehow that shows the difference between them. Like this.. $a = "php freaks"; $b = "phe"; Some how then do $a - $b and return "fraks" Im looking for basically letters that are in the first string that are NOT in the second one. Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/ Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 I don't know of a PHP function that can do that. Guess you'll have to do it the hard way. BTW, what happened to the space in between php and freaks? Did that get cutoff or something? Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/#findComment-833835 Share on other sites More sharing options...
cunoodle2 Posted May 14, 2009 Author Share Posted May 14, 2009 I don't know of a PHP function that can do that. Guess you'll have to do it the hard way. BTW, what happened to the space in between php and freaks? Did that get cutoff or something? I definitely made a mistake there. You are correct and the returned value should have been... " fraks" I figured I'd do it the hard way any way for fun but I know I'd save time if there was an existing php function for it. If anyone else happens to have a solution for this let me know. Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/#findComment-833840 Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 I don't know of anything like that, so here's what I would do. $a = "php freaks"; $b = "phe"; $b = explode('',$b); foreach ($b as $c) { $a = str_replace($c, '', $a); } echo $a; I think that should work. Edit - Or this: $a = "php freaks"; $b = "phe"; $c = strlen($b); for ($e = 0; $e < $c; $e++) { $a = str_replace($b[$e], '', $a); } echo $a; I believe that works too. Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/#findComment-833850 Share on other sites More sharing options...
cunoodle2 Posted May 14, 2009 Author Share Posted May 14, 2009 That worked like a charm but I initially got a few warnings... Warning: explode() [function.explode]: Empty delimiter in file.php on line 46 Warning: Invalid argument supplied for foreach() I changed the code to this.. $b = str_split($b); foreach ($b as $c) { $a = str_replace($c, '', $a); } echo $a; It works PERFECT. Thank you very much for the assistance!! Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/#findComment-833861 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 <?php $word1 = "php freaks"; $word2 = "phe"; echo str_replace(str_split(strtolower($word2)), '', strtolower($word1)); ?> I added strtolower to keep case out of it the mix (p != P) Quote Link to comment https://forums.phpfreaks.com/topic/158073-solved-compare-two-strings-and-return-value-of-differences-between-them/#findComment-833862 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.