Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: count of changed chars #76

Open
arilia opened this issue Jul 12, 2018 · 1 comment
Open

Feature: count of changed chars #76

arilia opened this issue Jul 12, 2018 · 1 comment
Labels

Comments

@arilia
Copy link

arilia commented Jul 12, 2018

Is there a way to have the count of the added/removed chars?

I need to show something like:

300 chars added, 600 chars removed

@arilia arilia changed the title Feature: count of ahcnged chars Feature: count of changed chars Jul 13, 2018
@ajquick
Copy link

ajquick commented Dec 9, 2019

This doesn't have to be done in the main code, though it certainly could be done through a stats() function or similar.

Here is generally how you would approach, but these are not the best coding practices:

$before = "before text here";
$after = "after text here";

$htmlDiff = new HtmlDiff($before, $after);
$content = $htmlDiff->build();

preg_match_all('/\<ins class\=\"([^"]+)\"\>([^<]+)\<\/ins\>/is', $content, $ins_matches);
preg_match_all('/\<del class\=\"([^"]+)\"\>([^<]+)\<\/del\>/is', $content, $del_matches);

$insert_count = 0;

for($i = 0; $i < count($ins_matches[1]); $i++){
    $insert_count += strlen($ins_matches[1][$i]);
}

$delete_count = 0;

for($i = 0; $i < count($del_matches[1]); $i++){
    $delete_count += strlen($del_matches[1][$i]);
}

echo $insert_count . ' chars added, ' . $delete_count .  ' chars removed';

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants