Simple text diff function I've put together this extremely simple text diff function a while ago:

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 &lt; $length; $i++) {
        if(!isset($new[$i])) {
            $result .= "&lt;del&gt;{$old[$i]}&lt;/del&gt;";
            continue;
        }

        if(!isset($old[$i])) {
            $result .= "&lt;ins&gt;{$new[$i]}&lt;/ins&gt; ";
            continue;
        }           

        if($old[$i] != $new[$i]) {
            $result .= "&lt;del&gt;{$old[$i]}&lt;/del&gt;&lt;ins&gt;{$new[$i]}&lt;/ins&gt; ";
            continue;
        }

        $result .= "{$new[$i]} ";
    }

    $result = str_replace(array("&lt;/ins&gt; &lt;ins&gt;", "&lt;/del&gt; &lt;del&gt;"), " ", $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 &lt;del&gt;World!!&lt;/del&gt;&lt;ins&gt;world!&lt;/ins&gt; Please &lt;del&gt;review&lt;/del&gt;&lt;ins&gt;critique&lt;/ins&gt; my diff function. Be &lt;del&gt;gentle.&lt;/del&gt;&lt;ins&gt;extra vigilant.&lt;/ins&gt;


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?
It looks fine. A few notes:

1, Use max instead of 

$length = count($old) &gt; 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 "&lt;del&gt;{$input}&lt;/del&gt;";
    }
    public function insert($input) {
        return "&lt;ins&gt;{$input}&lt;/ins&gt;";
    }
    public function removeUnnecessaryMarkers($input) {
        return str_replace(array("&lt;/ins&gt; &lt;ins&gt;", "&lt;/del&gt; &lt;del&gt;"), " ", $input); 
    }
}




...
if(!isset($new[$i])) {
    $result .= $decorator-&gt;delete($old[$i]);
    continue;
}
...


(I haven't tested that this is a valid PHP syntax or not. Feel free to fix it.)