Highlight the Difference Between Two Strings in PHP

Highlight the difference between two strings in PHP

You were able to use the PHP Horde_Text_Diff package.

However this package is no longer available.

compare and highlight difference of two strings containing html in php

You could try one of the following alternatives to the htmlDiff function:

  1. PHP inline dif mini-library which produces results like this
  2. Horde_Text_Diff library
  3. xdiff_string_diff function from xdiff extension

How to highlight changes/difference in one text paragraph from the other?

Options:

  • PHP Inline Diff (uses PEAR text-diff)
  • PEAR Text_Diff
  • Diff in Pure PHP
  • xdiff_string_diff()

Stackoverflow Archive:

  • Highlight the difference between two strings in PHP
  • Apply Diff in PHP
  • Need Help Optimizing php string difference function

Yii2 compare and highlight difference between two strings or text in gridview

Yes we can. It's not perfect but I can change it a little bit to suit my needs.

https://github.com/chrisboulton/php-diff/blob/master/example/example.php

So it's already there in yii, you don't have to install anything.

I've added the following to index.php:

require_once \Yii::$app->basePath . '/vendor/phpspec/php-diff/lib/Diff.php';
require_once \Yii::$app->basePath . '/vendor/phpspec/php-diff/lib/Diff/Renderer/Html/InlineMy.php';

Well the only problem is that none of the outputs are really perfect for me: the text renders are too few, the html renders are complete tables, and in a gridview it's not really nice, so I have made a copy of Inline.php as InlineMy.php and deleted all the stuff I don't need:

public function render() {
$changes = parent::render();
$html = '';
if (empty($changes)) {
return $html;
}

foreach ($changes as $i => $blocks) {
foreach ($blocks as $change) {
if ($change['tag'] == 'replace') {
foreach ($change['base']['lines'] as $no => $line) {
$html .= '<span style="white-space: nowrap">' . $line . '</span><br>';
}

foreach ($change['changed']['lines'] as $no => $line) {
$html .= '<span style="white-space: nowrap">' . $line . '</span>';
}
}
}
}
return $html;
}

furthermore, I have changed this in parent (Array.php):

<del> ==> <del style="background-color: red">
<ins> ==> <ins style="background-color: green">

I'm using at the moment SqlDataProvider, so my gridview looks a little bit different than usual (ActiveDataProvider):

[
'attribute' => 'diff',
'value' => function ($row) {
$diff = new Diff(explode("\n", $row["name"]), explode("\n", $row["name2"]));
$renderer = new Diff_Renderer_Html_Inline;
return $diff->render($renderer);
},
'format' => 'html',
],

Output:

something <== red

somewhat <== that's actually underlined and green, not bold

I'm sure one can make it more elegant but in a quick and dirty way it's fine for me at the moment because I don't want to build it furthermore.

PHP String Comparison then highlight the difference

Since this is array of JSONs you can first explode it (you can do it by new line char) to put every JSON in array and then do json_decode() for each of them. Then you will get two arrays which will consist of associative arrays.
All you have to do is to make the loops and compare if one array contains the other one and put the missing ones into third array.

Hope this helps...

highlighting difference in text

This is a cool problem.

Here's my solution:

extension NSMutableAttributedString {
func setCharacterColor(at location: Int) {
let range = NSRange(location: location, length: 1)
self.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
}
}


extension Array where Element == Character {
func index(of character: Character, greaterThan index: Int) -> Int? {
for i in index...self.count where self[i] == character {
return i
}
return nil
}
}


let correctString = "The soldiers stormed into the village just after sunrise"
let incorrectString = "solder stormed village just after sunrise"
let correctArray = Array(correctString)
let incorrectArray = Array(incorrectString)
var correctMutableString = NSMutableAttributedString(string: correctString)

var currentPosition = 0
for char in incorrectArray {
guard let position = correctArray.index(of: char, greaterThan: currentPosition) else {
continue
}
while currentPosition < position {
correctMutableString.setCharacterColor(at: currentPosition)
currentPosition = currentPosition + 1
}
currentPosition = position + 1
}

labelCorrect.attributedText = correctMutableString
labelIncorrect.attributedText = NSMutableAttributedString(string: incorrectString)

Sample Image

It's still order n squared but this problem is always going to be rather computensive.

It works by looping until it finds a character in the correct string where the character matches a character in the incorrect string. It then highlights all the characters it passes over during that process. The key is that it only highlights new characters never ones that it has already looped over previously.



Related Topics



Leave a reply



Submit