How to compare two texts with markup in js or php?

Good time, friends.
You need to compare two versions of the text. The text is the html markup. This can be done either php or js, whatever. Tried using prettyTextDiff, which is a wrapper for google_diff_match_patch.jsbut he can only work with pure text. Crutches and-so's mother begged him to work with markup, but it is terribly responsive. For example, if the text will be replaced by ul to ol, then displays uo. And here all the trouble. Found no adequate solutions, check the length of the remote/attached (>3 characters, for example) line breaks everything.

Here people faced with the same problem: a Click, so I will take from his example:

Text 1:
the <h1>Heading</h1>
Subtitle
Mom soap frame.
It was not tuch the sky.

Text 2:
the <h1>Title</h1>
<h2>Subtitle</h2>
My mother washed poodle.
In the sky were clouds.

The result:
<h1><span class=old>Title</span> <span class=new>Name</span><h1>
<h2>Subtitle</h2>
Mom soap <span class=old>frame</span> <span class=new>poodle</span>.
<span class=old>Not tuch the sky.</span>
<span class=new>In the sky were clouds.</span>
March 19th 20 at 08:32
2 answers
March 19th 20 at 08:34
Solution
Can someone also needed.
Made for: https://github.com/gorhill/PHP-FineDiff
To work with HTML you need to include "wordGranularity", insert spaces after and before the tags, and when you output run it through htmlspecialchars_decode($rendertext) that the tags are not shown text.

<?php
include $_SERVER['DOCUMENT_ROOT'] . '/assets/components/fineDiff/finediff.php';

$original_text = str_replace('>' , '> ', $original_text);
$original_text = str_replace('<' , ' <', $original_text);

$changed_text = str_replace('>' , '> ', $changed_text);
$changed_text = str_replace('<' , ' <', $changed_text);

$opcodes = FineDiff::getDiffOpcodes($original_text, $changed_text, FineDiff::$wordGranularity);


$rendertext = FineDiff::renderDiffToHTMLFromOpcodes($original_text, $opcodes);

return htmlspecialchars_decode($rendertext);
March 19th 20 at 08:36

Find more questions by tags Text processingPHP