查找两个字符串之间的第一个不同字符

给定两个长度相等的字符串,是否有一种优雅的方法来获得第一个不同字符的偏移量?

显而易见的解决办法是:

for ($offset = 0; $offset < $length; ++$offset) {
if ($str1[$offset] !== $str2[$offset]) {
return $offset;
}
}

但对于这样一个简单的任务来说,这看起来不太对。

15645 次浏览

您可以使用 按位异或(^)的一个很好的属性来实现这一点: 基本上,当您将两个字符串放在一起时,相同的字符将变成空字节("\0")。因此,如果我们用 xor 表示这两个字符串,我们只需要使用 strspn找到第一个非空字节的位置:

$position = strspn($string1 ^ $string2, "\0");

就是这样,我们来看一个例子:

$string1 = 'foobarbaz';
$string2 = 'foobarbiz';
$pos = strspn($string1 ^ $string2, "\0");


printf(
'First difference at position %d: "%s" vs "%s"',
$pos, $string1[$pos], $string2[$pos]
);

这将产生:

位置7的第一个区别是: “ a”和“ i”

应该可以了。它的 非常效率,因为它只使用 C 函数,并且只需要字符串的一个内存副本。

编辑: 同一行的多字节解决方案:

function getCharacterOffsetOfDifference($str1, $str2, $encoding = 'UTF-8') {
return mb_strlen(
mb_strcut(
$str1,
0, strspn($str1 ^ $str2, "\0"),
$encoding
),
$encoding
);
}

首先使用上述方法找到字节级别的差异,然后将偏移量映射到字符级别。这是使用 mb_strcut函数完成的,它基本上是 substr,但是尊重多字节字符边界。

var_dump(getCharacterOffsetOfDifference('foo', 'foa')); // 2
var_dump(getCharacterOffsetOfDifference('©oo', 'foa')); // 0
var_dump(getCharacterOffsetOfDifference('f©o', 'fªa')); // 1

它不像第一个解决方案那样优雅,但它仍然是一行程序(如果使用的默认编码更简单一些) :

return mb_strlen(mb_strcut($str1, 0, strspn($str1 ^ $str2, "\0")));

如果将字符串转换为单个字符一字节值的数组,则可以使用数组比较函数来比较字符串。

您可以使用以下方法获得类似于 XOR 方法的结果。

$string1 = 'foobarbaz';
$string2 = 'foobarbiz';


$array1 = str_split($string1);
$array2 = str_split($string2);


$result = array_diff_assoc($array1, $array2);


$num_diff = count($result);
$first_diff = key($result);


echo "There are " . $num_diff . " differences between the two strings. <br />";
echo "The first difference between the strings is at position " . $first_diff . ". (Zero Index) '$string1[$first_diff]' vs '$string2[$first_diff]'.";

编辑: 多字节解决方案

$string1 = 'foorbarbaz';
$string2 = 'foobarbiz';


$array1 = preg_split('((.))u', $string1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$array2 = preg_split('((.))u', $string2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);


$result = array_diff_assoc($array1, $array2);


$num_diff = count($result);
$first_diff = key($result);


echo "There are " . $num_diff . " differences between the two strings.\n";
echo "The first difference between the strings is at position " . $first_diff . ". (Zero Index) '$string1[$first_diff]' vs '$string2[$first_diff]'.\n";
string strpbrk ( string $haystack , string $char_list )

Strpbrk ()在干草堆字符串中搜索 char _ list。

返回值是 $haystack 的子字符串,它从第一个匹配的字符开始。 作为一个 API 函数,它应该是 zippy。然后循环一次,查找返回字符串的偏移量零,以获得偏移量。

我想添加这个作为评论的最佳答案,但我没有足够的点。

$string1 = 'foobarbaz';
$string2 = 'foobarbiz';
$pos = strspn($string1 ^ $string2, "\0");


if ($pos < min(strlen($string1), strlen($string2)){
printf(
'First difference at position %d: "%s" vs "%s"',
$pos, $string1[$pos], $string2[$pos]
);
} else if ($pos < strlen($string1)) {
print 'String1 continues with' . substr($string1, $pos);
} else if ($pos < strlen($string2)) {
print 'String2 continues with' . substr($string2, $pos);
} else {
print 'String1 and String2 are equal';
}