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

发布于 2024-11-02 07:44:50 字数 180 浏览 14 评论 0原文

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

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

那么我应该能够得到名称 - 年龄和 ABC - 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 Levenshtein distance, but can't figure it out. Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-11-09 07:44:50
<?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);
?>
<?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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文