How to compare strings with differences and similarties in php ?

In this article we’ll discusshow to compare strings with differences and similarities in php. Usually we can use strcmp() to compare strings but it returns binary as result. Here we want to show the differences and similarities.

This is a simple method to compare strings and find its differences and similarities.The differences and similarities will be highlighted with red and green colors respectively.Highlighting compare strings with different color will make you understand easier.

PHP :


<?php
function get_string_diff($old, $new, $get_similarity=false){
    $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, "similarity"=>$get_similarity);
}
$string_old = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
$string_new = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
$diff = get_string_diff($string_old, $string_new,true);
echo "<center>
<h2>String comparison</h2>
<table border=1 cellpadding=15>
    <tr align=center>
        <td>old</td>
        <td>new</td>
        <td>similarity</td>
    </tr>
    <tr>
        <td>".$diff['old']."</td>
        <td>".$diff['new']."</td>
        <td>".$diff['similarity']."</td>
    </tr>
</table></center>";
?>
  1. Create a function get_string_diff() with parameters $string_old – for old string, $string_new – for new string and an optional parameter true or false – this will return the similarities of both string if true passed.
  2. Find the no of similarities from start of strings as well as from the end in reverse manner. strspn() will return the number of found in the old string versus new string. Check this link for more info on strspn(). Use strrev() string function to reverse string.
  3. Find the difference between length of old string – $from_end , same way find difference between length of new string – $from_end. Use strlen() to find the string length.
  4. substr() will return substring from specified string with limit start to end which we passes.
  5. Find the new difference and old difference in the same way.
  6. Give inline css to highlight the differences.
  7. If third parameter , that is $get_similarity is true then show the similarities by taking the start and end strings.
  8. Return all the values as an array.
  9. Define old and new strings, call the function by passing strings
  10. Print array values.

Hope this example for compare strings will help you. For other PHP solutions please check PHP.

Leave A Comment