1

我想找出两个字符串之间的差异。例如,如果

line1 = "My name is ABC"
line2 = "My age is xyz"

然后我应该能够得到名称 - 年龄和 ABC - xyz 的差异。

我想我可以使用 Levenshtein 距离,但无法弄清楚。任何帮助是极大的赞赏。

4

1 回答 1

2
<?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);
?>
于 2011-04-20T09:42:22.580 回答