This is a problem with stackoverflow. Returns two strings of the same length and finds the first different character positions in the two strings. The general practice is as follows:
The code is as follows:
For ($ offset = 0; $ offset <$ length; ++ $ offset ){
If ($ str1 [$ offset]! ==$ Str2 [$ offset]) {
Return $ offset;
}
}
The best answer to the question below is the use of an exclusive or operator (^). I have never used this operator before, and I do not know where to use it. I learned it today.
Generally, when you perform an exclusive or operation on two strings, the exclusive or result of the same character is null (""). so we only need to find the first non-null ("") character.
The code is as follows:
$ Position = strspns ($ string1 ^ $ string2, "\ 0 ");
Obviously, this is a more elegant and efficient method. In addition, the answer is appended with a multi-byte character solution.
The code is as follows:
Function getCharacterOffsetOfDifference ($ str1, $ str2, $ encoding = 'utf-8 '){
Return mb_strlen ($ str1, $ encoding)
-Mb_strlen (
Mb_strcut (
$ Str1,
Strspns ($ str1 ^ $ str2, "\ 0 "),
Mb_strlen ($ str1, '8bit '),
$ Encoding
),
$ Encoding
);
}