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