<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A simple diff algorithm in PHP</title>
	<atom:link href="http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Mar 2010 11:57:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jens</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-20414</link>
		<dc:creator>Jens</dc:creator>
		<pubDate>Thu, 31 Dec 2009 12:51:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-20414</guid>
		<description>Paul, this is really a good portion of code in a nice way - thanks a lot. Could you please give us a link to the snipped gbruins mailed you? I am very interested in it. You could send it via mail too.
Wish you a happy new year!</description>
		<content:encoded><![CDATA[<p>Paul, this is really a good portion of code in a nice way &#8211; thanks a lot. Could you please give us a link to the snipped gbruins mailed you? I am very interested in it. You could send it via mail too.<br />
Wish you a happy new year!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tehem</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-11220</link>
		<dc:creator>Tehem</dc:creator>
		<pubDate>Tue, 06 Oct 2009 19:31:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-11220</guid>
		<description>Great code, really useful. I was searching for a text diff without having to install PEAR stuff, that&#039;s it. Thanks a LOT ! I&#039;m very grateful.</description>
		<content:encoded><![CDATA[<p>Great code, really useful. I was searching for a text diff without having to install PEAR stuff, that&#8217;s it. Thanks a LOT ! I&#8217;m very grateful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niraj</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-10485</link>
		<dc:creator>Niraj</dc:creator>
		<pubDate>Sat, 26 Sep 2009 01:06:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-10485</guid>
		<description>A beautiful use of recursion to solve the problem of computing diff!!</description>
		<content:encoded><![CDATA[<p>A beautiful use of recursion to solve the problem of computing diff!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-6301</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 05 Aug 2009 15:45:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-6301</guid>
		<description>Hi Paul,

I was told to manage a website that actually already uses your algorithm.
After redesigning the program logic and database to handle UTF8-data instead of iso latin1, I discover line breaks being displayed in a wrong way.

Is your algorithm UTF8-safe?


regards, Chris</description>
		<content:encoded><![CDATA[<p>Hi Paul,</p>
<p>I was told to manage a website that actually already uses your algorithm.<br />
After redesigning the program logic and database to handle UTF8-data instead of iso latin1, I discover line breaks being displayed in a wrong way.</p>
<p>Is your algorithm UTF8-safe?</p>
<p>regards, Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Willem</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-6177</link>
		<dc:creator>Willem</dc:creator>
		<pubDate>Sun, 02 Aug 2009 16:12:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-6177</guid>
		<description>Great script, thanks!</description>
		<content:encoded><![CDATA[<p>Great script, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henry</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-763</link>
		<dc:creator>Henry</dc:creator>
		<pubDate>Tue, 12 May 2009 18:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-763</guid>
		<description>How hard would it be to adapt this to display the strings side by side with differences highlighted, similar to the wikimedia difference engine?</description>
		<content:encoded><![CDATA[<p>How hard would it be to adapt this to display the strings side by side with differences highlighted, similar to the wikimedia difference engine?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Butler</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-67</link>
		<dc:creator>Paul Butler</dc:creator>
		<pubDate>Sun, 12 Apr 2009 15:09:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-67</guid>
		<description>Yeah, unfortunately WordPress doesn&#039;t work well with code in comments. Thanks for e-mailing the code, I&#039;ll have a look.</description>
		<content:encoded><![CDATA[<p>Yeah, unfortunately WordPress doesn&#8217;t work well with code in comments. Thanks for e-mailing the code, I&#8217;ll have a look.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gbruins</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-70</link>
		<dc:creator>gbruins</dc:creator>
		<pubDate>Thu, 09 Apr 2009 18:28:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-70</guid>
		<description>oops that didn&#039;t work out so well.    I guess Ill try emailing the code to Paul so he can post it if he wants to.   Sorry!</description>
		<content:encoded><![CDATA[<p>oops that didn&#8217;t work out so well.    I guess Ill try emailing the code to Paul so he can post it if he wants to.   Sorry!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gbruins</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-68</link>
		<dc:creator>gbruins</dc:creator>
		<pubDate>Thu, 09 Apr 2009 18:26:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-68</guid>
		<description>Great function Paul!   Thought this might help someone out...here  is the &quot;diff&quot; function and the presentation code to display the lines that are different, as well as the actual differences in each line highlighted:

(hoping that my code will remain intact here)

 $oldValue){
		$newKeys = array_keys($new, $oldValue);

		foreach($newKeys as $newIndex){
			$matrix[$oldIndex][$newIndex] = isset($matrix[$oldIndex - 1][$newIndex - 1]) ?
				$matrix[$oldIndex - 1][$newIndex - 1] + 1 : 1;
			if($matrix[$oldIndex][$newIndex] &gt; $maxlen){
				$maxlen = $matrix[$oldIndex][$newIndex];
				$oldMax = $oldIndex + 1 - $maxlen;
				$newMax = $newIndex + 1 - $maxlen;
			}
		}
	}

	if($maxlen == 0) {
	  $diffArray = array(array(&#039;deleted&#039;=&gt;$old, &#039;inserted&#039;=&gt;$new));
	} else {
  	$diffArray = array_merge(
        		      diff(array_slice($old, 0, $oldMax), array_slice($new, 0, $newMax)),
              		array_slice($new, $newMax, $maxlen),
              		diff(array_slice($old, $oldMax + $maxlen), array_slice($new, $newMax + $maxlen))
              	 );
	}
	return $diffArray;
}

$diffArray = diff($f2, $f1);
?&gt;


#diffTable {
  border-collapse: collapse;
}

#diffTable td {
 padding: 1px 5px 1px 10px;
 vertical-align: top;
}

td.lineNumber {
  border-right: 1px solid #ccc;
}

span.strDiff {
  background-color: #B6FF9F;
  padding: 0;
  margin: 0;
}




   $diff)	{
    if(!is_array($diff)) {
      ?&gt;

        &lt;!-- line # --&gt;


        &lt;!-- deleted --&gt;


        &lt;!-- spacer --&gt;
        &#160;

        &lt;!-- line # --&gt;


        &lt;!-- inserted --&gt;


       count($diff[&#039;inserted&#039;]) ? $diff[&#039;deleted&#039;] : $diff[&#039;inserted&#039;];
      foreach($biggest as $diffKey =&gt; $diffVal) {
        if(isset($diff[&#039;deleted&#039;][$diffKey]) &amp;&amp;
           isset($diff[&#039;inserted&#039;][$diffKey]) &amp;&amp;
           trim($diff[&#039;deleted&#039;][$diffKey]) == trim($diff[&#039;inserted&#039;][$diffKey])) {
             $newStr1 = $diff[&#039;deleted&#039;][$diffKey];
             $newStr2 = $diff[&#039;inserted&#039;][$diffKey];
             $backgroundStyle = &quot;&quot;;
        } else {
          //explode the strings so we can highlight the differences between strings
          $str1 = explode(&quot; &quot;, $diff[&#039;deleted&#039;][$diffKey]);
          $str2 = explode(&quot; &quot;, $diff[&#039;inserted&#039;][$diffKey]);
          $newStr1 = &quot;&quot;;
          $newStr2 = &quot;&quot;;

          if(count($str1) &gt;= count($str2)) {
            foreach($str1 as $strKey =&gt; $strVal) {
              if(isset($str1[$strKey]) &amp;&amp; isset($str2[$strKey])) {
                if($str1[$strKey] != $str2[$strKey]) {
                  $newStr1 .= &#039; &#039;.$str1[$strKey].&#039;&#039;;
                  $newStr2 .= &#039; &#039;.$str2[$strKey].&#039;&#039;;
                } else {
                  $newStr1 .= &#039; &#039;.$str1[$strKey];
                  $newStr2 .= &#039; &#039;.$str2[$strKey];
                }
              } elseif(isset($str1[$strKey]) &amp;&amp; !isset($str2[$strKey])) {
                $newStr1 .= &#039; &#039;.$str1[$strKey];
                $newStr2 .= &#039;&#039;;
              } else {
                $newStr1 .= &#039;&#039;;
                $newStr2 .= &#039; &#039;.$str2[$strKey];
              }
            }
          } else {
            foreach($str2 as $strKey =&gt; $strVal) {
              if(isset($str2[$strKey]) &amp;&amp; isset($str1[$strKey])) {
                if($str2[$strKey] != $str1[$strKey]) {
                  $newStr1 .= &#039; &#039;.$str1[$strKey].&#039;&#039;;
                  $newStr2 .= &#039; &#039;.$str2[$strKey].&#039;&#039;;
                } elseif(isset($str2[$strKey]) &amp;&amp; !isset($str1[$strKey])) {
                  $newStr2 .= &#039; &#039;.$str2[$strKey];
                  $newStr1 .= &#039;&#039;;
                } else {
                  $newStr2 .= &#039;&#039;;
                  $newStr1 .= &#039; &#039;.$str1[$strKey];
                }
              }
            }
          }
          $backgroundStyle = &quot;background-color: #FFCFCF;&quot;;
        }
        ?&gt;


          &lt;!-- line number --&gt;
          &quot;&gt;



          &lt;!-- deleted --&gt;
          &quot;&gt;

          &lt;!-- spacer --&gt;
          &#160;

          &lt;!-- line number --&gt;
          &quot;&gt;



          &lt;!-- inserted --&gt;
          &quot;&gt;</description>
		<content:encoded><![CDATA[<p>Great function Paul!   Thought this might help someone out&#8230;here  is the &#8220;diff&#8221; function and the presentation code to display the lines that are different, as well as the actual differences in each line highlighted:</p>
<p>(hoping that my code will remain intact here)</p>
<p> $oldValue){<br />
		$newKeys = array_keys($new, $oldValue);</p>
<p>		foreach($newKeys as $newIndex){<br />
			$matrix[$oldIndex][$newIndex] = isset($matrix[$oldIndex - 1][$newIndex - 1]) ?<br />
				$matrix[$oldIndex - 1][$newIndex - 1] + 1 : 1;<br />
			if($matrix[$oldIndex][$newIndex] &gt; $maxlen){<br />
				$maxlen = $matrix[$oldIndex][$newIndex];<br />
				$oldMax = $oldIndex + 1 &#8211; $maxlen;<br />
				$newMax = $newIndex + 1 &#8211; $maxlen;<br />
			}<br />
		}<br />
	}</p>
<p>	if($maxlen == 0) {<br />
	  $diffArray = array(array(&#8216;deleted&#8217;=&gt;$old, &#8216;inserted&#8217;=&gt;$new));<br />
	} else {<br />
  	$diffArray = array_merge(<br />
        		      diff(array_slice($old, 0, $oldMax), array_slice($new, 0, $newMax)),<br />
              		array_slice($new, $newMax, $maxlen),<br />
              		diff(array_slice($old, $oldMax + $maxlen), array_slice($new, $newMax + $maxlen))<br />
              	 );<br />
	}<br />
	return $diffArray;<br />
}</p>
<p>$diffArray = diff($f2, $f1);<br />
?&gt;</p>
<p>#diffTable {<br />
  border-collapse: collapse;<br />
}</p>
<p>#diffTable td {<br />
 padding: 1px 5px 1px 10px;<br />
 vertical-align: top;<br />
}</p>
<p>td.lineNumber {<br />
  border-right: 1px solid #ccc;<br />
}</p>
<p>span.strDiff {<br />
  background-color: #B6FF9F;<br />
  padding: 0;<br />
  margin: 0;<br />
}</p>
<p>   $diff)	{<br />
    if(!is_array($diff)) {<br />
      ?&gt;</p>
<p>        <!-- line # --></p>
<p>        <!-- deleted --></p>
<p>        <!-- spacer --><br />
        &nbsp;</p>
<p>        <!-- line # --></p>
<p>        <!-- inserted --></p>
<p>       count($diff['inserted']) ? $diff['deleted'] : $diff['inserted'];<br />
      foreach($biggest as $diffKey =&gt; $diffVal) {<br />
        if(isset($diff['deleted'][$diffKey]) &amp;&amp;<br />
           isset($diff['inserted'][$diffKey]) &amp;&amp;<br />
           trim($diff['deleted'][$diffKey]) == trim($diff['inserted'][$diffKey])) {<br />
             $newStr1 = $diff['deleted'][$diffKey];<br />
             $newStr2 = $diff['inserted'][$diffKey];<br />
             $backgroundStyle = &#8220;&#8221;;<br />
        } else {<br />
          //explode the strings so we can highlight the differences between strings<br />
          $str1 = explode(&#8221; &#8220;, $diff['deleted'][$diffKey]);<br />
          $str2 = explode(&#8221; &#8220;, $diff['inserted'][$diffKey]);<br />
          $newStr1 = &#8220;&#8221;;<br />
          $newStr2 = &#8220;&#8221;;</p>
<p>          if(count($str1) &gt;= count($str2)) {<br />
            foreach($str1 as $strKey =&gt; $strVal) {<br />
              if(isset($str1[$strKey]) &amp;&amp; isset($str2[$strKey])) {<br />
                if($str1[$strKey] != $str2[$strKey]) {<br />
                  $newStr1 .= &#8216; &#8216;.$str1[$strKey].&#8221;;<br />
                  $newStr2 .= &#8216; &#8216;.$str2[$strKey].&#8221;;<br />
                } else {<br />
                  $newStr1 .= &#8216; &#8216;.$str1[$strKey];<br />
                  $newStr2 .= &#8216; &#8216;.$str2[$strKey];<br />
                }<br />
              } elseif(isset($str1[$strKey]) &amp;&amp; !isset($str2[$strKey])) {<br />
                $newStr1 .= &#8216; &#8216;.$str1[$strKey];<br />
                $newStr2 .= &#8221;;<br />
              } else {<br />
                $newStr1 .= &#8221;;<br />
                $newStr2 .= &#8216; &#8216;.$str2[$strKey];<br />
              }<br />
            }<br />
          } else {<br />
            foreach($str2 as $strKey =&gt; $strVal) {<br />
              if(isset($str2[$strKey]) &amp;&amp; isset($str1[$strKey])) {<br />
                if($str2[$strKey] != $str1[$strKey]) {<br />
                  $newStr1 .= &#8216; &#8216;.$str1[$strKey].&#8221;;<br />
                  $newStr2 .= &#8216; &#8216;.$str2[$strKey].&#8221;;<br />
                } elseif(isset($str2[$strKey]) &amp;&amp; !isset($str1[$strKey])) {<br />
                  $newStr2 .= &#8216; &#8216;.$str2[$strKey];<br />
                  $newStr1 .= &#8221;;<br />
                } else {<br />
                  $newStr2 .= &#8221;;<br />
                  $newStr1 .= &#8216; &#8216;.$str1[$strKey];<br />
                }<br />
              }<br />
            }<br />
          }<br />
          $backgroundStyle = &#8220;background-color: #FFCFCF;&#8221;;<br />
        }<br />
        ?&gt;</p>
<p>          <!-- line number --><br />
          &#8220;&gt;</p>
<p>          <!-- deleted --><br />
          &#8220;&gt;</p>
<p>          <!-- spacer --><br />
          &nbsp;</p>
<p>          <!-- line number --><br />
          &#8220;&gt;</p>
<p>          <!-- inserted --><br />
          &#8220;&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kriplozoik</title>
		<link>http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/comment-page-1/#comment-69</link>
		<dc:creator>kriplozoik</dc:creator>
		<pubDate>Sun, 22 Mar 2009 17:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/a-simple-diff-algorithm-in-php/#comment-69</guid>
		<description>I was currently looking for something just like that - simple, independent on other libraries, not hundreds of lines of code.

Nice work and (as said above) thanks for releasing it.</description>
		<content:encoded><![CDATA[<p>I was currently looking for something just like that &#8211; simple, independent on other libraries, not hundreds of lines of code.</p>
<p>Nice work and (as said above) thanks for releasing it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
