Question

I have 2 strings that I'd like to compare, and return the positions of the different characters in the second string.

For example, if I have

  1. "The brown fox jumps over the lazy dog"
  2. "The quick brown fox jumped over the lazy dog"

I want it to highlight "quick" and "ed". What's the best way to go about this in PHP?

Was it helpful?

Solution

This might do the trick:

PHP Inline Diff

Text_Diff

OTHER TIPS

The algorithm you're looking for is the "longest common substring problem". From there it is easy to determine the differences. See Wikipedia:

http://en.wikipedia.org/wiki/Diff#Algorithm

This is going to give you a headache unless you define your porblem more clearly to start! Let's assume that str1 is "Amanda and Amy", and str2 is "Amanda and Amylase Amy".

Is your function to return "lase Amy" or "Amylase "?

Properly defining your problem is the first step towards a solution!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top