Oihana PHP

differingLines.php

Table of Contents

Functions

differingLines()  : int
Counts how far apart two texts are, in lines: the lines found on one side and not on the other.

Functions

differingLines()

Counts how far apart two texts are, in lines: the lines found on one side and not on the other.

differingLines(string $a, string $b) : int

The lines are compared as a multiset, without regard to their order: a line present twice on one side and once on the other counts one, a line changed counts two (the old one and the new one), and lines that only moved count nothing. The answer is zero only when the two texts are identical, byte for byte; any other difference — lines swapped, a trailing blank, a final line break — counts one at least, so "different" is never reported as zero.

Meant as a size, not a diff: "this copy differs from the reference by 12 lines" is enough to tell a cosmetic change from a rewrite, without showing either.

Parameters
$a : string

The first text, e.g. the reference.

$b : string

The second text, e.g. the copy.

Tags
example
use function oihana\core\strings\differingLines;

differingLines( "a\nb\n" , "a\nb\n" ) ;    // 0
differingLines( "a\nb\n" , "a\nc\n" ) ;    // 2 — b gone, c come
differingLines( "a\n"    , "a\nb\n" ) ;    // 1
differingLines( "a\nb\n" , "b\na\n" ) ;    // 1 — the same lines, moved
differingLines( "a\n"    , "a \n" ) ;      // 2 — a trailing blank is a difference
author

Marc Alcaraz (ekameleon)

since
1.4.0
Return values
int —

The number of lines on one side and not on the other; 1 at least when the texts differ.

On this page

Search results