String Comparison - Any Ideas?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hpskid
Forum Newbie
Posts: 4
Joined: Mon Aug 21, 2006 7:54 pm

String Comparison - Any Ideas?

Post by hpskid »

I've been brainstorming and writing code and deleting code and essentially going crazy for the past six hours trying to figure out a solution to the following problem. I challenge anyone to help unravel this issue - it'd be greatly appreciated.

Basically, I need a piece of PHP script that will compare two strings and highlight in red what is different in the second vs. the original.
For example:
Original String: I walked the dog. It was fun.
Modified String: Bob walked the cat. He said it was fun.

And the comparison would output the following (with the red, marking the changes):
Compared String: Bob walked the cat. He said it was fun.

Can anyone give me some pointers or actually show me a script that would do this? I've tried it using a combination of some while and for loops, and exploding the strings into arrays. I've somewhat got it, but I can't quite seem to get it right. Any help would be great. Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Any basic diff system should be able to show you that information. levenshtein() may be of interest, as could many different flavors of Wikiwiki out there.
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

Solution

Post by zeek »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

function whats_the_diff($str1, $str2) {

   $str1 = explode(' ', $str1);
   $str2 = explode(' ', $str2);

   for($x=0; $x<count($str2); $x++) {

      if($str1[$x] != $str2[$x]) {

         echo '<span style="color: red;">'.$str2[$x].' </span>';

      } else {

         echo $str2[$x].' ';

      }

   }

}

$str1 = "I walked the dog.";
$str2 = "Bob walked the cat.";

whats_the_diff($str1, $str2);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
hpskid
Forum Newbie
Posts: 4
Joined: Mon Aug 21, 2006 7:54 pm

Post by hpskid »

Thanks so much guys for leading me in the right direction! I ended up using the diff tool here (http://software.zuavra.net/inline-diff/), which requires the PHP diff module, and it works great. Thanks again for the pointers! :)
Post Reply