0

I've some PHP functions that given two JSON files, merge them into one to meet certain specs. However I need to add a feature, using PHP, that merges content strings of both files. These strings may have text that is different in some point between them.

Example

JSON-1

"elements":[

...
{
            "type": "text",            
            "content": "It went up the tree! Then climbed down.",           
        },
...
]

JSON-2

"elements":[

...
{
            "type": "text",            
            "content": "I saw your cat. It went up the tree! Then it run away!",           
        },
...
]

And I need the merged file to be like

JSON-MERGED

"elements":[

...
{
            "type": "text",            
            "content": "<la>I saw your cat.</la> It went up the tree! <po>Then climbed down.<po> <li> Then it run away!</li>",           
        },
...
]

Any idea on how could I do this? I thought of "exploding" the content in an array and then iterate over each other to see what matches. But then, similar words, in different context would match and mess up the code!

nalply
  • 26,770
  • 15
  • 78
  • 101
João Dias
  • 1,078
  • 1
  • 15
  • 38
  • Basically you want to use JS to diff the 2 strings. [This might help out](http://ejohn.org/projects/javascript-diff-algorithm/). – N.B. Aug 22 '12 at 14:31
  • 3
    http://stackoverflow.com/questions/321294/highlight-the-difference-between-two-strings-in-php – N.B. Aug 22 '12 at 14:43
  • The PHP Script in the question is out of date, and PEAR is giving me Deprecated reference problems, like `Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Text\Diff.php on line 427 ` – João Dias Aug 22 '12 at 15:22
  • Google > php string diff, problem solved. – N.B. Aug 22 '12 at 17:52

2 Answers2

-1

Give a try to Text_Diff: http://www.horde.org/libraries/Horde_Text_Diff/

udexter
  • 2,307
  • 10
  • 41
  • 58