"Merge Documents" in PHP

  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

I am trying to create a PHP script that will work somewhat like MS Word's "Merge documents."

Here's exactly what I want the script to do: You feed the script 2 snippets of code and it returns the similarities between the two,

For example, if you fed the script these two snippets of code:

Snippet #1:
Code: [ Select ]
if($var == true) {
   echo("The variable is true");
}
  1. if($var == true) {
  2.    echo("The variable is true");
  3. }


[Snippet #2:
Code: [ Select ]
if($var === true) {
   echo("This variable sure is true!");
  1. if($var === true) {
  2.    echo("This variable sure is true!");


Then, the script should return this:
Code: [ Select ]
if($var ==* true) {
   echo("* variable *is true*");
}
  1. if($var ==* true) {
  2.    echo("* variable *is true*");
  3. }


(the *s are where the two snippets differed)

Does anyone know how I would go about doing this?

I'd appreciate any help, links, or tips you could give me.

Thank you very much

~Caspian the 11th
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Moses08
  • Graduate
  • Graduate
  • Moses08
  • Posts: 196

Post 3+ Months Ago

I would visit the php.net page about Strings and see if their are functions you could use.

http://www.php.net/manual/en/ref.strings.php

You could use str_split to make the strings into arrays and then compare them using an if statement. That's a possible solution.
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Well... I already thought of doing a character-by character analyses, but then, if I got these two strings:

Code: [ Select ]

echo("hi!");
  1. echo("hi!");

and...
Code: [ Select ]

echo("well, hi!");
  1. echo("well, hi!");


The script will only detect this similarity:
Code: [ Select ]

echo("
  1. echo("


I also thought of splitting the 2 strings by "\n"s and comparing the array elements, but that wouldn't exactly work because if I did that to the two strings I used in my example (1st post), the only similarity it would return:

Code: [ Select ]

*
*
}
  1. *
  2. *
  3. }


Lines 1 and 2 differ slightly between these two examples, and I need to be able to detect those slight differences.
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Hmmm... I've been thinking about this, and the best way I can think to do it is to compare line-by line and extract all the lines that don't match, then do a character-by-character analysis of each of these lines.

Does anyone think this would work? It might be a little tricky to do...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 70 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.