Find the differences between two texts. Simplifies the problem by
stripping any common prefix or suffix off the texts before diffing.
public diff_main ( string $text1, string $text2, boolean $checklines = true ) : array | ||
$text1 | string | Old string to be diffed. |
$text2 | string | New string to be diffed. |
$checklines | boolean | Optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff. |
Résultat | array | Array of changes. |
public function actionDiff($document_history_id) { $model = History::findOne($document_history_id); if (is_null($model)) { throw new \yii\web\HttpException(404, Yii::t('burivuh', 'Record in the history does not exist')); } $document = Document::findOne($model->document_id); $previous = History::find()->where(['document_id' => $model->document_id])->andWhere('created_at<:created_at', [':created_at' => $model->created_at])->orderBy('created_at DESC')->limit(1)->one(); $diffs = []; if (!is_null($previous)) { $dmp = new DiffMatchPatch(); $diffs = $dmp->diff_main($previous->content, $model->content, false); } return $this->render('diff', ['document' => $document, 'model' => $model, 'previous' => $previous, 'diffs' => $diffs]); }
public function renderDiff($lastEdit) { $dmp = new DiffMatchPatch(); $diffs = $dmp->diff_main($this->content, $lastEdit->content, false); $added = 0; $deleted = 0; foreach ($diffs as $diff) { $length = mb_strlen($diff[1], 'utf-8'); if ($diff[0] == 1) { $added += $length; } if ($diff[0] == -1) { $deleted += $length; } } $this->diff = $added . ':' . $deleted; }