1

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?

Belinda
  • 1,230
  • 2
  • 14
  • 25
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • possible duplicate of [PHP text diff script](http://stackoverflow.com/questions/2178554/php-text-diff-script) – hsz Apr 15 '11 at 15:46
  • Could be a duplicate question: http://stackoverflow.com/q/321294/234695 – thetaiko Apr 15 '11 at 15:46

1 Answers1

1

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.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • uhm yeah, in fact I'd like to implement my own function for this kind of analysis :) Thanks anyway! – markzzz Apr 15 '11 at 22:31
  • @markzzz, "your own function" means "the fucntion you are asking us to do"? If you are not that good at this kind of thing let premade mini-framework do that for you. The functions you are requiring to be done are somehow difficult to realize, you should accept the fact that the frameworks are better sometimes: this is one of this times. (Notice that both the mini-framework are suggested are very lightweight) – Shoe Apr 16 '11 at 11:44