Compare two strings and show difference between them

2

I accidentally clicked a post around here that was edited and I saw that StackOverflow showed the edit differences. Soon, a lamp shone 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 whether it's different from the original or not, something far from str1 != str2 I want it to show even what has changed.

Audited .txt file

A100000000000000000010000199000000000000000
E000000000000000000010000111000JARED-LETO--

System-generated .txt file (this being the original)

A000000000000000000020000129000000000000000
E000011110000000000010000441000HEATH-LEDGER

What I expect

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

Above an example, changing the differences by + .

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

    
asked by anonymous 03.08.2016 / 03:11

1 answer

1

There are several tools that do this, but 'manually' can do this:

function get_str_difs($str1, $str2) {
    $old = htmlentities($str1);
    $new = htmlentities($str2);
    $from_start = strspn($old ^ $new, "
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);
}
"); $from_end = strspn(strrev($old) ^ strrev($new), "
$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>';
"); $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 ended up finding another simple way to do it:

function get_str_difs($str1, $str2) {
    $old = htmlentities($str1);
    $new = htmlentities($str2);
    $from_start = strspn($old ^ $new, "
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);
}
"); $from_end = strspn(strrev($old) ^ strrev($new), "
$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>';
"); $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); }

To use the above functions:

%pre%

Note that the two have different mechanics / results, it's a matter of seeing which one you prefer

    
03.08.2016 / 14:02