Hey There Developers I am Trying to Mark the changed words in two Strings i.e, I am trying to highlight the changed word in the string and display it in a box with green highlighted text and the old string in another box with red highlighted text This is the code I am using
<?php
function get_string_diff($old, $new, $get_similarity=false){
// echo $old;
$from_start = strspn($old ^ $new, "");
$from_end = strspn(strrev($old) ^ strrev($new), "");
$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";
if($get_similarity)
$get_similarity = "<ins style='background-color:#ccffcc'>$start $end</ins>";
return array("old"=>$old, "new"=>$new);
}
$string_old = "This is the second div that we are using to check if the original data can be seen & Yes It is Working";
$string_new = "This is the first div that we are using to check if the changed data can be seen & Yes It is Working";
$diff = get_string_diff($string_old, $string_new,true);
echo "<center>
<h2>Old String VS New String</h2>
<table border=1 cellpadding=15>
<tr align=center>
<td>Old</td>
<td>New</td>
</tr>
<tr>
<td>".$diff['old']."</td>
<td>".$diff['new']."</td>
</tr>
</table></center>";
?>
Although it is working to some extent as the output is something like this
The changed words in the strings are "Second" & "Original" So Ideally it should Highlight red only those two & not all the words between the two.
2
Answers
As per your question trying to compare a word to a word you don’t need to use a substring to do so, it’s sufficient to compare a whole word to another. You can try the below function
This is the tested output
In order to find the difference between the two strings, we can consider each individual words as a single token and then do the comparison by exploding them on space character.
For comparison, we can go with finding the
Longest Common Subsequence(LCS)
among the 2 strings. This will yield us a minimal set of words that are different between the two strings which I presume is the most important for this task.Once the LCS length is found, we will go with finding the actual words(set of tokens) which contribute to this length. These set of tokens will be common words among the 2 strings in order.
Our last step is to simply do a one-to-one comparison check with each word in
old
version of the string and innew
version of the string. For old version of string, we will surround the not found token withdel
tags andins
for the new version. The common words will remain untouched as desired.Snippet:
Online Demo
HTML View