PHP - Differences between two pieces of text
I have two pieces of text! Every piece of text has some lines. Every line contains any sort of char/numbers;
I'd like to implement a PHP function that will tell :
- if a line has been deleted or inserted from the original one;
- if a line has been edited;
Example :
Text 1 Text 2 Result my name is marco my name is marco OK I'm 25 I'm 26 Return 1 I like football I hate baseball Make newline on Text 2 I hate baseball I'm cold I'm cold OK
I tried many ways to do it, but I don't know how to identify if a line is NEW or if it's just edited. Maybe I should trace some index when user write the text开发者_JS百科?
You could use the PEAR package that find differences trough the text: PEAR Text Diff
PEAR Text Diff: Engine for performing and rendering text diffs
There's also Simple Diff which I don't personally know, IMO it's more trustable the PEAR package.
Simple Diff: Given two arrays, the function diff will return an array of the changes. I won't describe the format of the array, but it will be obvious if you use print_r() on the result of a diff on some test data.
精彩评论