src/Diff.php
public function xIndex($loc)
    {
        $diffs = $this->getChanges();

        $chars1 = 0;
        $chars2 = 0;
        $last_chars1 = 0;
        $last_chars2 = 0;

        $i = 0;
        foreach ($diffs as $change) {
            $op = $change[0];
            $text = $change[1];
            // Equality or deletion.
            if ($op != self::INSERT) {
                $chars1 += mb_strlen($text);
            }
            // Equality or insertion.
            if ($op != self::DELETE) {
                $chars2 += mb_strlen($text);
            }
            // Overshot the location.
            if ($chars1 > $loc) {
                break;
            }
            $last_chars1 = $chars1;
            $last_chars2 = $chars2;
            $i++;
        }

        // The location was deleted.
        if (count($diffs) != $i && $diffs[$i][0] == self::DELETE) {
            return $last_chars2;
        }
        return $loc + $last_chars2 - $last_chars1;
    }
public function xIndex($loc)
    {
        $diffs = $this->getChanges();

        $chars1 = 0;
        $chars2 = 0;
        $last_chars1 = 0;
        $last_chars2 = 0;

        $i = 0;
        foreach ($diffs as $change) {
            $op = $change[0];
            $text = $change[1];
            // Equality or deletion.
            if ($op != self::INSERT) {
                $chars1 += mb_strlen($text);
            }
            // Equality or insertion.
            if ($op != self::DELETE) {
                $chars2 += mb_strlen($text);
            }
            // Overshot the location.
            if ($chars1 > $loc) {
                break;
            }
            $last_chars1 = $chars1;
            $last_chars2 = $chars2;
            $i++;
        }

        // The location was deleted.
        if (count($diffs) != $i && $diffs[$i][0] == self::DELETE) {
            return $last_chars2;
        }
        return $loc + $last_chars2 - $last_chars1;
    }
Compute and return location in text2 equivalent to the $loc in text1.
e.g. "The cat" vs "The big cat", 1->1, 5->8

@param int $loc Location within text1.

@return int Location within text2.