How do you find the difference between two lines in PHP?

I have 2 lines that I would like to compare, and return the positions of different characters in the second line.

For example, if I have

  • "The brown fox jumps over the lazy dog"
  • "The quick brown fox jumped over the lazy dog"

I want it to highlight "fast" and "ed". What is the best way to do this in PHP?

+5
source share
3 answers

This can do the trick:

PHP Inline Diff

Text_diff

+4
source

The algorithm you are looking for is "the longest common substring problem." From there it’s easy to spot the differences. See Wikipedia:

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

+3
source

, , ! , str1 - " ", str2 - " ".

"lase Amy" "Amylase"?

- !

+3
source

All Articles