downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/function.xdiff-string-diff.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/ref.xdiff.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'function.xdiff-string-diff.php',
   
1 => 'xdiff_string_diff',
  ),
 
'up' =>
  array (
   
0 => 'ref.xdiff.php',
   
1 => 'xdiff Functions',
  ),
 
'prev' =>
  array (
   
0 => 'function.xdiff-string-diff-binary.php',
   
1 => 'xdiff_string_diff_binary',
  ),
 
'next' =>
  array (
   
0 => 'function.xdiff-string-merge3.php',
   
1 => 'xdiff_string_merge3',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="function.xdiff-string-diff" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">xdiff_string_diff</h1>
  <p class="verinfo">(PECL xdiff &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">xdiff_string_diff</span> &mdash; <span class="dc-title">Make unified diff of two strings</span></p>

 </div>
 
 <a name="function.xdiff-string-diff.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><b>xdiff_string_diff</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$old_data</tt></span>
   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$new_data</tt></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$context</tt><span class="initializer"> = 3</span></span>
   [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$minimal</tt><span class="initializer"> = false</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Makes an unified diff containing differences between <i><tt class="parameter">old_data</tt></i>
 string and
   <i><tt class="parameter">new_data</tt></i>
 string and returns it. The resulting diff is human-readable.
   An optional <i><tt class="parameter">context</tt></i>
 parameter specifies how many lines of context should be
   added around each change (with default value of 3). Setting <i><tt class="parameter">minimal</tt></i>
 parameter
   to true will result in outputting the shortest patch file possible (can take a long time).
  </p>
 </div>


 <a name="function.xdiff-string-diff.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">old_data</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       First string with data. It acts as &quot;old&quot; data.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">new_data</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Second string with data. It acts as &quot;new&quot; data.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">context</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Indicates how many lines of context you want to include in the diff
       result. Default is 3.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">minimal</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Set this parameter to <b><tt class="constant">TRUE</tt></b> if you want to minimalize the size of the
       result (can take a long time).
      </p>
     </dd>

   
   </dl>
<p>
  </p>
 </div>


 <a name="function.xdiff-string-diff.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns string with resulting diff or <b><tt class="constant">FALSE</tt></b> if an internal error happened.
  </p>
 </div>


 <a name="function.xdiff-string-diff.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Example #1 <b>xdiff_string_diff()</b> example</b></p>
    <div class="example-contents para"><p>
     The following code makes unified diff of two articles.
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$old_article&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./old_article.txt'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$new_article&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'article'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">/*&nbsp;Let's&nbsp;say&nbsp;that&nbsp;someone&nbsp;pasted&nbsp;a&nbsp;new&nbsp;article&nbsp;to&nbsp;html&nbsp;form&nbsp;*/<br /><br /></span><span style="color: #0000BB">$diff&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xdiff_string_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$old_article</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$new_article</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$diff</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Differences&nbsp;between&nbsp;two&nbsp;articles:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
 </div>


 <a name="function.xdiff-string-diff.notes"></a><div class="refsect1 notes">
  <h3 class="title">Notes</h3>
  <blockquote><p><b class="note">Note</b>:
  
    This function doesn&#039;t work well with binary strings. To make diff of binary
    strings use <a href="function.xdiff-string-bdiff.php" class="function">xdiff_string_bdiff()</a>/<a href="function.xdiff-string-rabdiff.php" class="function">xdiff_string_rabdiff()</a>.
   <br />
  </p></blockquote>
 </div>


 <a name="function.xdiff-string-diff.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="function.xdiff-string-patch.php" class="function" rel="rdfs-seeAlso">xdiff_string_patch()</a> - Patch a string with an unified diff</li>
   </ul><p>
  </p>
 </div>

</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites