difftext

Show readers exactly what changed.

Diff components for Vue, React, and PHP. Compare text by character, word, sentence, or line, or compare rich HTML without breaking its markup.

composer require sitefinitysteve/php-diff-text
Orders leave our Torontheo warehouse at 67am and usually arrive within threewo business days. Tracking links are emailed once the parcel is scanned.
21 characters added7 characters removed120 characters unchanged85% similar
DiffText::chars()
Orders leave theour Toronto warehouse at 6am7am and usually arrive within threetwo business days. Tracking links are emailed once the parcel is scanned.
25 characters added11 characters removed116 characters unchanged85% similar
DiffText::words()
Orders leave theour Toronto warehouse at 6am7am and usually arrive within threetwo business days. Tracking links are emailed once the parcel is scanned.
25 characters added11 characters removed116 characters unchanged85% similar
DiffText::wordsWithSpace()
Orders leave the warehouse at 6am and arrive within three business days.Orders leave our Toronto warehouse at 7am and usually arrive within two business days. Tracking links are emailed once the parcel is scanned.
86 characters added72 characters removed55 characters unchanged85% similar
DiffText::sentences()
Orders leave the warehouse at 6am and arrive within three business days. Tracking links are emailed once the parcel is scanned.Orders leave our Toronto warehouse at 7am and usually arrive within two business days. Tracking links are emailed once the parcel is scanned.
141 characters added127 characters removed0 characters unchanged85% similar
DiffText::lines()

Pick the right granularity

Each mode breaks the text into different pieces before comparing. Smaller pieces catch typos. Larger pieces keep rewrites readable.

Characters

DiffText::chars()

Typos, digits, and single-letter fixes.

−
Recieve your refund within 5 buisness days.
+
Receive your refund within 3 business days.
Recieive your refund within 53 buisiness days.

Words

DiffText::words()

Reworded copy, where spacing doesn't matter.

−
The weekly sync starts at 9am in the east meeting room.
+
The weekly standup starts at 9:30am in the north meeting room.
The weekly syncstandup starts at 9am9:30am in the eastnorth meeting room.

Words + whitespace

DiffText::wordsWithSpace()

Edits where spacing and indentation count.

−
total = price * qty if total > 100: apply(discount)
+
total = price * quantity if total >= 100: apply(discount)
total = price * qtyquantity if total >= 100: apply(discount)

Sentences

DiffText::sentences()

Paragraph edits, reviewed one sentence at a time.

−
Parking is free after 6pm. Guests should check in at the front desk. The pool closes at 10pm.
+
Parking is free after 6pm. Guests can check in at the front desk or with the mobile app. The pool closes at 10pm.
Parking is free after 6pm. Guests should check in at the front desk.Guests can check in at the front desk or with the mobile app. The pool closes at 10pm.

Lines

DiffText::lines()

Lists, configs, and anything written one item per line.

−
2 cups flour 1 tsp baking soda 1 cup sugar 2 eggs 1 cup milk
+
2 cups flour 1 tsp baking powder 3/4 cup sugar 2 eggs 1 cup oat milk 1 tsp vanilla
2 cups flour 1 tsp baking soda 1 cup sugar 1 tsp baking powder 3/4 cup sugar 2 eggs 1 cup milk 1 cup oat milk 1 tsp vanilla

HTML

DiffText::html()

Rich text from an editor, with the markup kept intact.

−
<p>Returns are accepted within <strong>14 days</strong> of delivery.</p><ul><li>Items must be unused</li><li>Keep the original box</li></ul>
+
<p>Returns are accepted within <strong>30 days</strong> of delivery.</p><ul><li>Items must be unused</li><li>Keep the original box</li><li>Sale items are final</li></ul>

Returns are accepted within 1430 days of delivery.

  • Items must be unused
  • Keep the original box
  • Sale items are final

Review a whole document

For longer text, compare line by line. Unchanged stretches fold away, and edited lines show which words changed.

Side by side

DiffText::split()
1# Store policy
1# Store policy
2
2
3Hours
3Hours
4Monday to Friday, 9am to 6pm.
4Monday to Friday, 9am to 7pm.
5Saturday, 10am to 4pm.
5Saturday, 10am to 4pm.
6Closed Sundays and holidays.
6Sunday, 11am to 3pm.
7
7
8Returns
8Returns
9Returns are accepted within 14 days.
9Returns are accepted within 30 days.
10Items must be unused and in the original box.
10Items must be unused and in the original box.
11Refunds go back to the original payment method.
11Refunds go back to the original payment method.
12Gift cards cannot be refunded.
12Gift cards cannot be refunded.
13
13
14Shipping
14Shipping
15Orders ship from our warehouse.
15Orders ship from our Toronto warehouse.
16Standard shipping takes three business days.
16Standard shipping takes two business days.
17Express shipping is available at checkout.
17Express shipping is available at checkout.
18We do not ship to PO boxes.
18We do not ship to PO boxes.
19
19
20Contact
20Contact
21Email support@example.com.
21Email support@example.com.
22Phone lines are open during store hours.
22Phone lines are open during store hours.
23Live chat is available on our website.

Unified, with folded context

6 lines added5 lines removed17 lines unchanged89% similar
DiffText::unified()
11# Store policy
22
33Hours
4Monday to Friday, 9am to 6pm.
4Monday to Friday, 9am to 7pm.
55Saturday, 10am to 4pm.
6Closed Sundays and holidays.
6Sunday, 11am to 3pm.
77
88Returns
9Returns are accepted within 14 days.
9Returns are accepted within 30 days.
1010Items must be unused and in the original box.
1111Refunds go back to the original payment method.
1212Gift cards cannot be refunded.
1313
1414Shipping
15Orders ship from our warehouse.
16Standard shipping takes three business days.
15Orders ship from our Toronto warehouse.
16Standard shipping takes two business days.
1717Express shipping is available at checkout.
1818We do not ship to PO boxes.
2 unchanged lines
2121Email support@example.com.
2222Phone lines are open during store hours.
23Live chat is available on our website.

Use it

// composer require sitefinitysteve/php-diff-text
use PhpDiffText\DiffText;

echo DiffText::words($before, $after);
echo DiffText::split($before, $after, ['contextLines' => 3]);
echo DiffText::stats($before, $after, ['mode' => 'unified']);
// then link css/style.css from the package