Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:33:57+00:00 2026-06-10T20:33:57+00:00

I’m still working on an online SVN tool, and got stuck again at diff

  • 0

I’m still working on an online SVN tool, and got stuck again at diff calculations this time.

I have made a test file test.txt that gives me this result on diff:

Index: C:/data/aaxc/test.txt
===================================================================
--- C:/data/aaxc/test.txt   (revision 8)
+++ C:/data/aaxc/test.txt   (working copy)
@@ -1,3 +1,5 @@
-Fully new line
+Fully new line 1
{2nd modified line}
Specia$ čhar līne
+
+Nice one!
\ No newline at end of file

After that, I’m creating an array:

$data = explode( "\n", $svn_result );

$result = array();
for ( $k=2; $k<sizeof($data); $k++ ) {

    # checks for filename
    if ( substr( $data[$k], 0, 3 ) == '---' ) $result['left'] = substr( $data[$k], 4 );
    else if ( substr( $data[$k], 0, 3 ) == '+++' ) $result['right'] = substr( $data[$k], 4 );

    # check for changes
    else if ( substr( $data[$k], 0, 1 ) == '-' ) $result['-'][] = substr( $data[$k], 1 );
    else if ( substr( $data[$k], 0, 1 ) == '+' ) $result['+'][] = substr( $data[$k], 1 );

}

And the output:

Array
(
[left] => C:/data/aaxc/test.txt (revision 8)
[right] => C:/data/aaxc/test.txt    (working copy)
[-] => Array
    (
        [0] => Fully new line
    )

[+] => Array
    (
        [0] => Fully new line 1
        [1] => 
        [2] => Nice one!
    )

)

So far so good, but how can I now make sure witch line has been changed and to what? Because currently, when I try to highlight the changes, I can’t in no way be sure that it will hightlight it correctly.

Maybe there is a script that already does this?

enter image description here

Currently it is working fine on small changes, but will definitely fail on big ones.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:33:59+00:00Added an answer on June 10, 2026 at 8:33 pm

    You’ll need to store the line positions that are given on the 5th line of the diff, the one beginning with @@.

    So you could do:

    $data = explode( "\n", $svn_result );
    
    $result = array();
    for ( $k=2; $k<sizeof($data); $k++ ) {
    
        # checks for filename
        if ( substr( $data[$k], 0, 3 ) == '---' ) {
            $result['left'] = substr( $data[$k], 4 );
        } else if ( substr( $data[$k], 0, 3 ) == '+++' ) {
            $result['right'] = substr( $data[$k], 4 );
    
        # stores line starting positions
        } else if ( substr( $data[$k], 0, 2 ) == '@@') {
            // Remove @ symbols and trim whitespace
            $diff_line_nums = explode( ' ', trim( str_replace( '@@', '', $data[$k] ) ) );
            // Split by the comma
            $left_nums = explode( ',', $diff_line_nums[0] );
            $left_diff = array('line' => abs( $left_nums[0] ) ,
                                    'length' => $left_nums[1] );
            // Set the counter for this specific part of the diff back to 0
            $left_line_count = 0;
    
            // Split by the comma
            $right_nums = explode( ',', $diff_line_nums[1] );
            $right_diff = array('line' => abs( $right_nums[0] ) ,
                                     'length' => $right_nums[1] );
            // Set the counter for this specific part of the diff back to 0
            $right_line_count = 0;
    
        # check for changes
        } else if ( substr( $data[$k], 0, 1 ) == '-' ) {
            $result['-'][ $left_diff['line'] + $left_line_count ] = substr( $data[$k], 1 );
            $left_line_count++;
        } else if ( substr( $data[$k], 0, 1 ) == '+' ) {
            $result['+'][ $right_diff['line'] + $right_line_count ] = substr( $data[$k], 1 );
            $right_line_count++;
    
        // Otherwise assume there is no difference, so increment both left 
        // and right line counters
        } else {
            $right_line_count++;
            $left_line_count++;
        }
    }
    

    Which will give you output like:

    array (
        'left' => 'C:/data/aaxc/test.txt   (revision 8)',
        'right' => 'C:/data/aaxc/test.txt   (working copy)',
        '-' => array (
                1 => 'Fully new line'
               ),
        '+' => array (
                1 => 'Fully new line 1',
                4 => '', 
                5 => 'Nice one!'
               )
    )
    

    And then when you loop through the $result array you will know which line changes have been made on by the index key:

    foreach ($result['-'] as $line_number => $change) {
        // display removal changes
    }
    foreach ($result['+'] as $line_number => $change) {
        // display insert changes
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.