找出两个字符串之间的多个差异

时间:2011-04-20 09:35:58

标签: design-patterns distance levenshtein-distance

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

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

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

我想我可以使用Levenshtein距离,但无法弄明白。非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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);
?>