开发者

PHP - Differences between two pieces of text

开发者 https://www.devze.com 2023-02-26 02:49 出处:网络
I have two pieces of text! Every piece of text has some lines. Every line contains any sort of char/numbers;

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 :

  1. if a line has been deleted or inserted from the original one;
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号