比较 2 个字符串并显示差异(php 或 javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4609994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare 2 strings and display difference (php or javascript)
提问by Glavi?
Comparing must work on-the-fly, so this must be done via javascript or with php (will make javascript ajax request).
比较必须即时进行,因此必须通过 javascript 或 php 完成(将发出 javascript ajax 请求)。
What I need is probably advanced difference showing, so it will be probably best if there is any kind of good supported library.
我需要的可能是高级差异显示,所以如果有任何一种良好的支持库可能是最好的。
This picture shows exactly what I need >
这张照片正是我需要的 >


Added #1:I found this http://ejohn.org/projects/javascript-diff-algorithm/(example http://www.djsipe.com/js-diff/) which is ok, but doesn't support multiline? and it changes the whole word when only one character is different...
添加 #1:我发现这个http://ejohn.org/projects/javascript-diff-algorithm/(例如http://www.djsipe.com/js-diff/),这很好,但不支持多行? 当只有一个字符不同时,它会改变整个单词......
Added #2:I tested php script ( https://github.com/paulgb/simplediff/) but it has flaws.
添加 #2:我测试了 php 脚本(https://github.com/paulgb/simplediff/),但它有缺陷。
Added #3:I found what i was looking for ( http://code.google.com/p/google-diff-match-patch/)
添加 #3:我找到了我要找的东西(http://code.google.com/p/google-diff-match-patch/)
采纳答案by mmarinero
This answer to a related question seems promising for javascript. google-diff-match-patch
这个相关问题的答案似乎对 javascript 很有希望。谷歌差异匹配补丁
It provides an API which will take care of all the complex and well known algorithms. However you'll have to do some work with the presentation.
它提供了一个 API,它将处理所有复杂和众所周知的算法。但是,您必须对演示文稿做一些工作。
PHP is more versatile there is a lot of diff tools out there. Look this other related question Calculate text diffs in PHP.
PHP 更通用,有很多差异工具。看看这个其他相关问题Calculate text diffs in PHP。

