DiffMatchPatch\Diff::main PHP Method

main() public method

Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.
public main ( string $text1, string $text2, boolean $checklines = true, integer $deadline = null ) : self
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$checklines boolean Optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff.
$deadline integer Optional time when the diff should be complete by. Used internally for recursive calls. Users should set $this->timeout instead.
return self
    public function main($text1, $text2, $checklines = true, $deadline = null)
    {
        // Set a deadline by which time the diff must be complete.
        if (!isset($deadline)) {
            if ($this->getTimeout() <= 0) {
                $deadline = PHP_INT_MAX;
            } else {
                $deadline = microtime(1) + $this->getTimeout();
            }
        }
        if (!isset($text1, $text2)) {
            throw new \InvalidArgumentException();
        }
        // Check for equality (speedup).
        if ($text1 == $text2) {
            if ($text1 != '') {
                $this->setChanges(array(array(self::EQUAL, $text1)));
                return $this;
            }
            return $this;
        }
        $prevInternalEncoding = mb_internal_encoding();
        if ($prevInternalEncoding != 'UCS-2LE') {
            mb_internal_encoding('UCS-2LE');
            $text1 = iconv($prevInternalEncoding, 'UCS-2LE', $text1);
            $text2 = iconv($prevInternalEncoding, 'UCS-2LE', $text2);
        }
        // Trim off common prefix (speedup).
        $commonLength = $this->getToolkit()->commonPrefix($text1, $text2);
        if ($commonLength == 0) {
            $commonPrefix = '';
        } else {
            $commonPrefix = mb_substr($text1, 0, $commonLength);
            $text1 = mb_substr($text1, $commonLength);
            $text2 = mb_substr($text2, $commonLength);
        }
        // Trim off common suffix (speedup).
        $commonLength = $this->getToolkit()->commonSuffix($text1, $text2);
        if ($commonLength == 0) {
            $commonSuffix = '';
        } else {
            $commonSuffix = mb_substr($text1, -$commonLength);
            $text1 = mb_substr($text1, 0, -$commonLength);
            $text2 = mb_substr($text2, 0, -$commonLength);
        }
        // Compute the diff on the middle block.
        $diffs = $this->compute($text1, $text2, $checklines, $deadline);
        // Restore the prefix and suffix.
        if ($commonPrefix != '') {
            array_unshift($diffs, array(self::EQUAL, $commonPrefix));
        }
        if ($commonSuffix != '') {
            array_push($diffs, array(self::EQUAL, $commonSuffix));
        }
        if ($prevInternalEncoding != 'UCS-2LE') {
            mb_internal_encoding($prevInternalEncoding);
            foreach ($diffs as &$change) {
                $change[1] = iconv('UCS-2LE', $prevInternalEncoding, $change[1]);
            }
            unset($change);
        }
        $this->setChanges($diffs);
        $this->cleanupMerge();
        return $this;
    }

Usage Example

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require __DIR__ . "/../../src/DiffMatchPatch/Diff.php";
require __DIR__ . "/../../src/DiffMatchPatch/DiffToolkit.php";
require __DIR__ . "/../../src/DiffMatchPatch/Utils.php";
use DiffMatchPatch\Diff;
$size = 'M';
$text1 = file_get_contents(__DIR__ . "/fixtures/{$size}_performance1.txt");
$text2 = file_get_contents(__DIR__ . "/fixtures/{$size}_performance2.txt");
//$text1 = "The quick brown fox jumps over the lazy dog.";
//$text2 = "That quick brown fox jumped over a lazy dog.";
$timeStart = microtime(1);
$diff = new Diff();
$diff->setTimeout(0);
$diff->main($text1, $text2, false)->cleanupSemantic();
$timeElapsed = microtime(1) - $timeStart;
echo 'Elapsed time: ' . round($timeElapsed, 3) . PHP_EOL;
echo 'Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . PHP_EOL;
echo 'Texts length: ' . mb_strlen($text1) . ', ' . mb_strlen($text2) . PHP_EOL;
echo 'Diffs count: ' . count($diff->getChanges()) . PHP_EOL . PHP_EOL;
$timeStart = microtime(1);
$diff = new Diff();
$diff->setTimeout(0);
$diff->main($text1, $text2)->cleanupEfficiency();
$timeElapsed = microtime(1) - $timeStart;
echo 'Elapsed time: ' . round($timeElapsed, 3) . PHP_EOL;
echo 'Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . PHP_EOL;
echo 'Texts length: ' . mb_strlen($text1) . ', ' . mb_strlen($text2) . PHP_EOL;
echo 'Diffs count: ' . count($diff->getChanges()) . PHP_EOL . PHP_EOL;