Reputation: 41
I have a code to show difference between two sentences. But i need to check without case sensitive.
<?php
function get_decorated_diff($old, $new){
$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);
}
$string_old = "Hello World!";
$string_new = "hello world!";
$diff = get_decorated_diff($string_old, $string_new);
echo "<table>
<tr>
<td>".$diff['old']."</td>
<td>".$diff['new']."</td>
</tr>
</table>";
?>
It shows diffrence for Hello
and hello
. But i need to show difference without case sensitive
Upvotes: 1
Views: 5437
Reputation: 133400
you could set to lowercase at the begin of the function
function get_decorated_diff($old, $new){
$old = strlower($old);
$new = strlower($new);
$from_start = strspn($old ^ $new, "\0");
.......
if you need to preserve the originale values you can make a copy for return later the original content
Upvotes: 1
Reputation: 310
Try to make all strings in one case, as example
function get_decorated_diff($old, $new){
$originalOld = $old;
$originalNew = $new;
$old = strtolower($old); //Add this line
$new = strtolower($new); //Add this line
$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($originalNew, $from_start, $new_end - $from_start);
$old_diff = substr($originalOld, $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);
}
Upvotes: 1
Reputation: 5878
Since ('a' ^ 'A') = ' ' i.e. space character, you can use this code to compare regardless of case:
$from_start = strspn($old ^ $new, "\0 ");
$from_end = strspn(strrev($old) ^ strrev($new), "\0 ");
However, it will also have false matches when the characters to compare are not letters.
Upvotes: 0
Reputation: 20206
made it lowercase and do comparison
$old_lower = strlower($old);
$new_lower = strlower($new);
$from_start = strspn($old_lower ^ $new_lower, "\0");
To compare strings with case-insensitively, use strcasecmp
:
<?php
$string_old = "Hello World!";
$string_new = "hello world!";
if (strcasecmp($string_old, $string_new) == 0) {
echo '$string_old is equal to $string_new in a case-insensitive string comparison';
}
else {
echo = 'there is difference between $string_old with $string_new';
}
?>
Upvotes: 0