开发者

Find multiple differences between two strings

开发者 https://www.devze.com 2023-02-28 02:24 出处:网络
I want to find the differences between two strings. For example, if line1 = \"My name is ABC\" line2 = \"My age is xyz\"

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

精彩评论

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