PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 638 All time: 5,002 This week: 49Up
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  
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 (3)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:638
This week:0
All time:5,002
This week:49Up