Compare two strings and show difference between them

Asked

Viewed 743 times

2

I accidentally clicked on a post It was edited here and I saw that Stackoverflow showed the differences of the edition. Soon, a lamp flashed over my head and now that I’m dealing with a CVT project, I thought of an audit feature of the generated files, but not only showing if it’s different from the original or not, something far from str1 != str2, I want you to show even what has been changed.

File . txt sent for audit

A100000000000000000010000199000000000000000
E000000000000000000010000111000JARED-LETO--

File . txt generated by the system (this being the original)

A000000000000000000020000129000000000000000
E000011110000000000010000441000HEATH-LEDGER

What do I expect

A+000000000000000000+00001+9000000000000000
E0000++++0000000000010000++1000+++++-++++++

Above an example, exchanging the differences for +.

I had thought of doing the code mapping position by position of each character of the two files, but still would be different from how it is here in the forum. I also looked for something on github and did not find. Someone may have a solution for this ?

1 answer

3


It has several tools that do this, but 'manually' can do so:

function get_str_difs($str1, $str2) {
    $old = htmlentities($str1);
    $new = htmlentities($str2);
    $from_start = strspn($old ^ $new, "\0");        
    $from_end = strspn(strrev($old) ^ strrev($new), "\0");

    $old_end = strlen($old) - $from_end;
    $new_end = strlen($new) - $from_end;

    $start = substr($new, 0, $from_start);
    $end = substr($new, $new_end);
    $new_diff = substr($new, $from_start, $new_end - $from_start);  
    $old_diff = substr($old, $from_start, $old_end - $from_start);

    $new = "$start<ins style='background-color:#ccffcc'>$new_diff</ins>$end";
    $old = "$start<del style='background-color:#ffcccc'>$old_diff</del>$end";
    return array('old' => $old, 'new' => $new);
}

I finally figured out another simple way to do it:

function get_str_difs($str1, $str2) {
    $first = explode(" ", $str1);
    $second = explode(" ", $str2);
    $arrDif1 = array_diff($first,$second);
    $arrDif2 = array_diff($second,$first);

    $old = '';
    $new = '';
    foreach($first as $word) {
        if(in_array($word,$arrDif1)) {
            $old .= "<del style='background-color:#ffcccc'>" . $word . "</del> ";
            continue;
        }
        $old .= $word . " ";     
    }
    foreach($second as $word) {
        if(in_array($word,$arrDif2)) {
            $new .= "<ins style='background-color:#ccffcc'>" . $word . "</ins> ";
            continue;
        }
        $new .= $word . " ";   
    }
    return array('old' => $old, 'new' => $new);
}

To use the above functions:

$str1 = "Olá, cá estou eu no Stack Overflow PT";
$str2 = "Hey, cá estou eu no Stack Exchange, SO PT";
$difs = get_str_difs($str1, $str2);
echo '<p>Str1:<b>' .$str1. '</b></p>';
echo '<p>Str2:<b>' .$str2. '</b></p>';
echo '<p><b>Difference:</b></p>';
echo '<p>' .$difs['old']. '</p>';
echo '<p>' .$difs['new']. '</p>';

Note that the two have different mechanics/results, it is a question of which prefer

  • Thank you @Miguel, I was only able to test the codes now, it worked and I have implemented in my application, I got the result I wanted. Feral embrace !

  • And put right away without testing?! hehe of anything @Williamnovak, glad it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.