Highlight the difference between two strings in PHP

前端 未结 13 2447
独厮守ぢ
独厮守ぢ 2020-11-22 07:50

What is the easiest way to highlight the difference between two strings in PHP?

I\'m thinking along the lines of the Stack Overflow edit history page, where new text

13条回答
  •  囚心锁ツ
    2020-11-22 08:34

    This is a nice one, also http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/

    Solving the problem is not as simple as it seems, and the problem bothered me for about a year before I figured it out. I managed to write my algorithm in PHP, in 18 lines of code. It is not the most efficient way to do a diff, but it is probably the easiest to understand.

    It works by finding the longest sequence of words common to both strings, and recursively finding the longest sequences of the remainders of the string until the substrings have no words in common. At this point it adds the remaining new words as an insertion and the remaining old words as a deletion.

    You can download the source here: PHP SimpleDiff...

提交回复
热议问题