In PHP, compare two strings to find the first character location. The general practice is as follows: Copy the code as follows :? Phpfor ($ offset0; $ offset $ length; ++ $ offset) {if ($ str1 [$ offset]! $ Str2 [$ offset]) {return $ offset;} 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 ("\ 0 "), so we only need to find the first non-null ("\ 0") 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
);
}
The pipeline code is as follows :? Php for ($ offset = 0; $ offset $ length; ++ $ offset) {if ($ str1 [$ offset]! ==$ Str2 [$ offset]) {return $ offset...