php - 如何使用 php 比较 2 个字符串并确定是否至少有 4 个字符发生变化

标签 php regex

我需要能够比较 string1"qwerty1234"和 string2"qwerty5678"这样的 2 个字符串,并能够确定 string2 是否至少有 4 个字符与 string1 相比发生了变化。

例如: qwerty1234 和 qwerty1235 仅更改了 1 个字符。 qwerty1234 和 qwerty5678 有 4 个字符的变化。 qwerty1234 和 qazxcv1234 有 4 个字符的变化。 qwerty1234 和 qwerty 有 4 个字符的变化。 qwerty1234 和 asdf123%^& 至少有 4 个字符的变化。

if (strcmp($cur_password, $new_password) <= 3)
{
    echo "Your new password is too similiar to your old password!"
    echo "There must be at least a 4 character difference.  Try again."
    break;
}

任何帮助将不胜感激。

谢谢!

最佳答案

您可以通过 levenstein function 获得差异:

The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.

Code :

$s1 = 'qwerty1234';
$s2 = 'qwerty5678';
echo levenshtein ($s1, $s2); // => 4

关于php - 如何使用 php 比较 2 个字符串并确定是否至少有 4 个字符发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638065/

相关文章:

php - 如何在 PHP 中测试连通性?

java - 使用 Java 替换来自阿拉伯语推文的表情符号 Unicode 范围

java - 如何匹配以或等于搜索字符串开头的 TreeMap 值

regex - Azure-CLI/Powershell 密码要求

php - 获取一个条目的多个日期之间的平均时间

php - 未找到接口(interface) 'Silex\ServiceProviderInterface'

java - 计算字符串正则表达式中的字母

html - HTML 正则表达式的替代品

javascript - JS 只使用一个 id 而忽略其他

javascript - 从具有相同值的多个复选框中捕获值