Viewed   65 times

Are there any libraries (3rd party or built-in) in PHP to calculate text diffs?



What sort of diffs? File diffs? There is array_diff() which acts on arrays. Then there is also xdiff, which "enables you to create and apply patch files containing differences between different revisions of files.". The latter acts on files or strings.

Edit: I should add xdiff doesn't appear to be out in a release yet. You have to build from source to use it.

Saturday, December 3, 2022

Could you not compare the time stamps instead?

$now = new DateTime('now');
$diff = $date->getTimestamp() - $now->getTimestamp()
Tuesday, December 20, 2022

Here's a nice little function that will add up any number of times passed in an array:-

function addTimes(Array $times)
    $total = 0;
    foreach($times as $time){
        list($hours, $minutes, $seconds) = explode(':', $time);
        $hour = (int)$hours + ((int)$minutes/60) + ((int)$seconds/3600);
        $total += $hour;
    $h = floor($total);
    $total -= $h;
    $m = floor($total * 60);
    $total -= $m/60;
    $s = floor($total * 3600);
    return "$h:$m:$s";

Use it like this:-

$times = array('12:39:25', '08:22:10', '11:08:50', '07:33:05',);


string '39:43:30' (length=8)
Monday, August 29, 2022

The php xdiff extension supports patching from both files and strings. Looking at the source for Text_Diff I see that it uses xdiff for performing the diffs. You should be able to patch using xdiff_string_patch() or some of it's sibling functions.

Saturday, September 17, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :