为此目的,有两个有趣的原生函数,
similar_text()和
levenshtein()
similar_text()的示例:
$string1 = 'AAAb';
$string2 = 'aaab';
similar_text ( $string1, $string2, $percentege );
echo $percentege . '%'; // Output: 25%
levenshtein()的例子:
levenshtein($string1, $string2, 1, 1000, 1000000);
编辑1
考虑到第一个字符串总是与第二个字符串具有相同数量的条目,您可以尝试以下代码.我为测试目的创建了两个字符串,第二个字符串有两个相等的条目和7个不同的条目,总共9个.
$first_string = '253.0.0.1,253.0.0.2,253.0.0.3,253.0.0.4,253.0.0.5,253.0.0.6,253.0.0.7,253.0.0.8,253.0.0.9';
$second_string = '253.0.0.1,253.0.0.2,255.255.255.127,255.255.255.128,255.255.255.129,255.255.255.130,255.255.255.131,255.255.255.132,255.255.255.133';
$first_array = explode(',', $first_string);
$second_array = explode(',', $second_string);
$total_entries = count($first_array);
$array_differences = array_diff($first_array, $second_array);
$different_entries = count($array_differences);
$percentage = ( $different_entries / $total_entries ) * 100 ;
echo 'Difference: ' . round($percentage, 2) . '%';