Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:58:02+00:00 2026-05-25T16:58:02+00:00

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

  • 0

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 ($offset = 0; $offset < $length; ++$offset) {
    if ($str1[$offset] !== $str2[$offset]) {
        return $offset;
    }
}

But that doesn’t look quite right, for such a simple task.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:58:02+00:00Added an answer on May 25, 2026 at 4:58 pm

    You can use a nice property of bitwise XOR (^) to achieve this: Basically, when you xor two strings together, the characters that are the same will become null bytes ("\0"). So if we xor the two strings, we just need to find the position of the first non-null byte using strspn:

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

    That’s all there is to it. So let’s look at an example:

    $string1 = 'foobarbaz';
    $string2 = 'foobarbiz';
    $pos = strspn($string1 ^ $string2, "\0");
    
    printf(
        'First difference at position %d: "%s" vs "%s"',
        $pos, $string1[$pos], $string2[$pos]
    );
    

    That will output:

    First difference at position 7: “a” vs “i”

    So that should do it. It’s very efficient since it’s only using C functions, and requires only a single copy of memory of the string.

    Edit: A MultiByte Solution Along The Same Lines:

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

    First the difference at the byte level is found using the above method and then the offset is mapped to the character level. This is done using the mb_strcut function, which is basically substr but honoring multibyte character boundaries.

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

    It’s not as elegant as the first solution, but it’s still a one-liner (and if you use the default encoding a little bit simpler):

    return mb_strlen(mb_strcut($str1, 0, strspn($str1 ^ $str2, "\0")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two interface references obtained from different sources. Is there a programmatic way to
Given two strings of equal length such that s1 = ACCT s2 = ATCT
Is there an algorithm that given two strings yields the degree of equality between
Given two date ranges, what is the simplest or most efficient way to determine
Given two tables of equal structure, but one with 100M rows and another with
Example: given two dates below, finish is always greater than or equal to start
How can I get the number of weekdays between two given dates without just
Given 2 strings, I want to find the first match of at least four
Here's a simple problem - given two urls, is there some built-in method, or
What's the simplest way to perform a set subtraction given two arrays in C#?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.