1
我想查找兩個字符串之間的差異。例如,如果查找兩個字符串之間的多個區別
line1 = "My name is ABC"
line2 = "My age is xyz"
然後,我應該能夠得到名稱 - 年齡和ABC - xyz的差異。
我想我可以使用Levenshtein距離,但無法弄清楚。任何幫助是極大的讚賞。
我想查找兩個字符串之間的差異。例如,如果查找兩個字符串之間的多個區別
line1 = "My name is ABC"
line2 = "My age is xyz"
然後,我應該能夠得到名稱 - 年齡和ABC - xyz的差異。
我想我可以使用Levenshtein距離,但無法弄清楚。任何幫助是極大的讚賞。
<?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);
?>
這將無法正常工作,因爲第一個區別在長度上不匹配:'name:4'和'age:3' – froeschli 2011-04-21 12:24:36