Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    san marcos TX
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    file comparison - diffs

    I'm attempting to write a function that will compare 2 input files line by line and output the differences as a readable page, with blocks of different lines outlined and displayed twice.. and the blocks that are the same displayed only once.. I've managed to get it to compare each line and output the differences, however i can't figure out how to find where the differences end.. IE it works fine if only one line is changed. however if i add an extra line to file 1, each line after that is shown as changed, when it really isn't. I'll be glad to provide more details if this doesn't make sense, but any help here would be greatly appreciated.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm it seems simple enough, you just read each file into the array and use strcmp() to see if they are the same, if they're different then you output both strings, if not then you output one and then the loop just goes onto the next line.

    Where is it that you are having trouble? fancy posting some example code?
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    san marcos TX
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, i've got it as far as what you mentioned, however i'm running into problems if the changes involve more than one line.. if file 1 is a 10 line file and file 2 is that same as file one except with a new line inserted at line 5, the line by line comparison thinks that all the rest of the lines are off. what i need is a way to check forward in both files to find a place where the contents start to match up again, output differing blocks of text. I'd include some code but all i've got is what you just described.. something like this:

    Code:
    while($x<= $highest_line_number){	
    
    	 if (!($contents[0][$x] == $contents[1][$x])){
    		echo "mismatch line $x<br>";
    		}	
    $x++;
    }

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    first off , I personally hate it when someone says 'theres no need to reinvent the wheel'

    however ... in this case , unless this is just a project .. perhaps theres no need to reinv.. etc

    on unix there is the diff command which can be exec()'d & there are windows ports of diff (google) available
    also google for the diff man page which shows you all the options

    eg
    PHP Code:
    <?
        exec
    'diff -y file2  file2 ' $yaks) ;
        echo 
    '<pre>';
        
    print_r$yaks ) ;
    ?>
    of course you may have reasons for wanting to roll your own , if not try the above.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Aug 2002
    Location
    san marcos TX
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually i would love to use something like that, but unfortunately it's not going to work in the context of this project.. ultimately i'm writing a script that needs to work on any server w/ apache and php, so relying on programs that may or may not be installed isn't going to be acceptable.. guess i'll just keep hacking away at it, i think i'm making some progress. if i can get it done in a small function i'll post it here.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •