PHP Classes

PHP String Diff Class: Compare and find differences between two strings

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 637 This week: 1All time: 5,001 This week: 560Up
Version License PHP version Categories
string-diff 1.0.0GNU Lesser Genera...3.0HTML, Algorithms, PHP 5, Text processing
Description 

Author

This class can compare and find differences between two strings.

It takes two strings and split them into arrays so it can find the differences by compare the arrays. It supports comparing UTF-8 encoding strings.

The class can return a HTML string with <del> and <ins> tags to show what characters should be inserted or deleted to transform one string in the other.

Picture of Philipp Strazny
  Performance   Level  
Name: Philipp Strazny is available for providing paid consulting. Contact Philipp Strazny .
Classes: 5 packages by
Country: United States United States
Age: 57
All time rank: 1380201 in United States United States
Week rank: 411 Up48 in United States United States Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<html>
<body>
<?php
include('StringDiff.php');
print
StringDiff::getCSS();
$a = 'This is a string.';
$b = 'This would be another string.';
print
'<table>';
print
'<tr><td colspan=2>English characters:</td></tr>';
print
'<tr><td>a:</td><td>'.$a.'</td></tr>';
print
'<tr><td>b:</td><td>'.$b.'</td></tr>';
print
'<tr><td>diff:</td><td>'.StringDiff::diff($a, $b).'</td></tr>';
print
'<tr><td>overlap in a:</td><td>'.StringDiff::overlap($a, $b).'</td></tr>';
print
'<tr><td>overlap in b:</td><td>'.StringDiff::overlap($b, $a).'</td></tr>';
print
'<tr></tr>';
print
'<tr><td colspan=2>Chinese characters:</td></tr>';
$a = '??????? ???????';
$b = '??????? ?????????';
print
'<tr><td>a:</td><td>'.$a.'</td></tr>';
print
'<tr><td>b:</td><td>'.$b.'</td></tr>';
print
'<tr><td>diff:</td><td>'.StringDiff::diff($a, $b).'</td></tr>';
print
'<tr><td>overlap in a:</td><td>'.StringDiff::overlap($a, $b).'</td></tr>';
print
'<tr><td>overlap in b:</td><td>'.StringDiff::overlap($b, $a).'</td></tr>';
print
'<tr></tr>';
print
'<tr><td colspan=2>Russian characters:</td></tr>';
$a = '??????????? ???????? ???? ?? ??????????? ????????? ?????? ';
$b = '??????????? ??????? ???? ? ??????';
print
'<tr><td>a:</td><td>'.$a.'</td></tr>';
print
'<tr><td>b:</td><td>'.$b.'</td></tr>';
print
'<tr><td>diff:</td><td>'.StringDiff::diff($a, $b).'</td></tr>';
print
'<tr><td>overlap in a:</td><td>'.StringDiff::overlap($a, $b).'</td></tr>';
print
'<tr><td>overlap in b:</td><td>'.StringDiff::overlap($b, $a).'</td></tr>';
print
'<tr></tr>';
print
'<tr><td colspan=2>Thai characters:</td></tr>';
$a = '?????????????? \'??????? ???.\' ????? \'?????????\' ???? \'???????\'';
$b = '?????????????? \'??????? .\' ????? \'?????????\' ???? \'??????????\'';
print
'<tr><td>a:</td><td>'.$a.'</td></tr>';
print
'<tr><td>b:</td><td>'.$b.'</td></tr>';
print
'<tr><td>diff:</td><td>'.StringDiff::diff($a, $b).'</td></tr>';
print
'<tr><td>overlap in a:</td><td>'.StringDiff::overlap($a, $b).'</td></tr>';
print
'<tr><td>overlap in b:</td><td>'.StringDiff::overlap($b, $a).'</td></tr>';
print
'</table>';


?>
</body>
</html>


  Files folder image Files  
File Role Description
Plain text file StringDiff.php Class the StringDiff class; use StringDiff::diff or StringDiff::overlap methods
Accessible without login Plain text file StringDiffTest.php Test testing all relevant functionality
Accessible without login Plain text file example.php Example usage examples: English, Chinese, Russian, Thai characters

 Version Control Unique User Downloads Download Rankings  
 0%
Total:637
This week:1
All time:5,001
This week:560Up