开发者

Find multiple differences between two strings

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);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜