Answer the question
In order to leave comments, you need to log in
How to compare two texts with markup in js or php?
Good time for friends.
You need to compare two versions of the text. The text contains html markup . You can do this with either php or js, it doesn't matter. Tried with prettyTextDiff , which is a wrapper for google_diff_match_patch.js , but it can only work with pure text. With crutches and such and such a mother, he begged him to work with the markup, but he reacts terribly to it. For example, if ul is replaced by ol in texts, then uo will be displayed. And this is where the trouble is. I did not find any adequate solution, checking the length of a deleted / added (> 3 characters, for example) line breaks everything.
Here a person faced the same problem: Click , so I’ll take his example right away:
Текст 1:
<h1>Заголовок</h1>
Подзаголовок
Мама мыла раму.
Не было тучь на небе.
Текст 2:
<h1>Название</h1>
<h2>Подзаголовок</h2>
Мама мыла пуделя.
В небе были тучи.
Результат:
<h1><span class=old>Заголовок</span> <span class=new>Название</span><h1>
<h2>Подзаголовок</h2>
Мама мыла <span class=old>раму</span> <span class=new>пуделя</span>.
<span class=old>Не было тучь на небе.</span>
<span class=new>В небе были тучи.</span>
Answer the question
In order to leave comments, you need to log in
Maybe someone will need it too.
Made on: https://github.com/gorhill/PHP-FineDiff
To work with HTML, you need to enable "wordGranularity", insert spaces after and before the tags, and run through htmlspecialchars_decode($rendertext) in the output so that the tags are not displayed as 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);
DOMDocument class
https://www.php.net/manual/en/class.domdocument.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question