patternphpMinor
Simple text diff function
Viewed 0 times
textsimplefunctiondiff
Problem
I've put together this extremely simple text diff function a while ago:
The html output is:
Hello World!!world! Please reviewcritique my diff function. Be gentle.extra vigilant.
The purpose of the function wasn't something important, as I can't even remember why I built it (found in my sandbox directory, which is full of little experiments). Is it any good? I've always used PEAR Text_Diff for text diffs, but if I'm not doing anything inherently wrong in my simple function, I'd love to use it instead.
Any suggestions on an easy way to move html out of the function?
function diff($old, $new) {
$old = trim($old);
$new = trim($new);
if($old == $new) {
return $new;
}
$old = explode(" ", $old);
$new = explode(" ", $new);
$result = "";
$length = max(count($old), count($new));
for($i = 0; $i {$old[$i]}";
continue;
}
if(!isset($old[$i])) {
$result .= "{$new[$i]} ";
continue;
}
if($old[$i] != $new[$i]) {
$result .= "{$old[$i]}{$new[$i]} ";
continue;
}
$result .= "{$new[$i]} ";
}
$result = str_replace(array(" ", " "), " ", $result);
return trim($result);
}
$string1 = "Hello World!! Please review my diff function. Be gentle.";
$string2 = "Hello world! Please critique my diff function. Be extra vigilant.";
echo diff($string1, $string2);
// Hello World!!world! Please reviewcritique my diff function. Be gentle.extra vigilant.The html output is:
Hello World!!world! Please reviewcritique my diff function. Be gentle.extra vigilant.
The purpose of the function wasn't something important, as I can't even remember why I built it (found in my sandbox directory, which is full of little experiments). Is it any good? I've always used PEAR Text_Diff for text diffs, but if I'm not doing anything inherently wrong in my simple function, I'd love to use it instead.
Any suggestions on an easy way to move html out of the function?
Solution
It looks fine. A few notes:
1, Use
2, Consider testing with newlines, tabs and maybe other whitespace characters.
3, http://en.wikipedia.org/wiki/Longest_common_subsequence could be useful if you want a better algorithm.
4, For removing HTML out of the function create a class which does it and delegate the calls to it. For example:
(I haven't tested that this is a valid PHP syntax or not. Feel free to fix it.)
1, Use
max instead of $length = count($old) > count($new) ? count($old) : count($new);2, Consider testing with newlines, tabs and maybe other whitespace characters.
3, http://en.wikipedia.org/wiki/Longest_common_subsequence could be useful if you want a better algorithm.
4, For removing HTML out of the function create a class which does it and delegate the calls to it. For example:
public interface Decorator {
public function delete($input);
public function insert($input);
public function removeUnnecessaryMarkers($input);
}
public class HtmlDecorator {
public function delete($input) {
return "{$input}";
}
public function insert($input) {
return "{$input}";
}
public function removeUnnecessaryMarkers($input) {
return str_replace(array(" ", " "), " ", $input);
}
}...
if(!isset($new[$i])) {
$result .= $decorator->delete($old[$i]);
continue;
}
...(I haven't tested that this is a valid PHP syntax or not. Feel free to fix it.)
Code Snippets
$length = count($old) > count($new) ? count($old) : count($new);public interface Decorator {
public function delete($input);
public function insert($input);
public function removeUnnecessaryMarkers($input);
}
public class HtmlDecorator {
public function delete($input) {
return "<del>{$input}</del>";
}
public function insert($input) {
return "<ins>{$input}</ins>";
}
public function removeUnnecessaryMarkers($input) {
return str_replace(array("</ins> <ins>", "</del> <del>"), " ", $input);
}
}...
if(!isset($new[$i])) {
$result .= $decorator->delete($old[$i]);
continue;
}
...Context
StackExchange Code Review Q#6352, answer score: 2
Revisions (0)
No revisions yet.