I want to find the differences between two strings. For example, if
line1 = "My name is ABC"
line2 = "My age is xyz"
Then I should be able to get the differences that name - age and ABC - xyz.
I think I can use Levensht开发者_运维百科ein distance, but can't figure it out. Any help is greatly appreciated.
<?php
$line1 = "My name is ABC";
$line2 = "My age is xyz";
$matchlen = strspn($line1, $line2);
// remove 1st non-matching char
$same = substr($line1, 0, $matchlen - 1);
// include 1st non-matching char
$diff = substr($line2, $matchlen - 1);
printf("Same: [%s]\nDiff: [%s]", $same, $diff);
?>
精彩评论