Find first character that is different between two strings

后端 未结 4 1201
心在旅途
心在旅途 2020-11-28 20:14

Given two equal-length strings, is there an elegant way to get the offset of the first different character?

The obvious solution would be:

for ($offs         


        
4条回答
  •  醉话见心
    2020-11-28 20:40

    string strpbrk ( string $haystack , string $char_list )
    

    strpbrk() searches the haystack string for a char_list.

    The return value is the substring of $haystack which begins at the first matched character. As an API function it should be zippy. Then loop through once, looking for offset zero of the returned string to obtain your offset.

提交回复
热议问题