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-patch.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-patch.php',
   
1 => 'xdiff_string_patch',
  ),
 
'up' =>
  array (
   
0 => 'ref.xdiff.php',
   
1 => 'xdiff Functions',
  ),
 
'prev' =>
  array (
   
0 => 'function.xdiff-string-patch-binary.php',
   
1 => 'xdiff_string_patch_binary',
  ),
 
'next' =>
  array (
   
0 => 'function.xdiff-string-rabdiff.php',
   
1 => 'xdiff_string_rabdiff',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="function.xdiff-string-patch" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">xdiff_string_patch</h1>
  <p class="verinfo">(PECL xdiff &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">xdiff_string_patch</span> &mdash; <span class="dc-title">Patch a string with an unified diff</span></p>

 </div>
 
 <a name="function.xdiff-string-patch.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_patch</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$patch</tt></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span>
   [, <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&amp;$error</tt></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Patches a <i><tt class="parameter">str</tt></i>
 string with an unified patch in <i><tt class="parameter">patch</tt></i>
 parameter
   and returns the result. <i><tt class="parameter">patch</tt></i>
 has to be an unified diff created by
   <a href="function.xdiff-file-diff.php" class="function">xdiff_file_diff()</a>/<a href="function.xdiff-string-diff.php" class="function">xdiff_string_diff()</a> function.
   An optional <i><tt class="parameter">flags</tt></i>
 parameter specifies mode of operation. Any
   rejected parts of the patch will be stored inside <i><tt class="parameter">error</tt></i>
 variable if
   it is provided.
  </p>
 </div>


 <a name="function.xdiff-string-patch.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">str</tt></i>
</span>

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

      <p class="para">
       The original string.
      </p>
     </dd>

   
    <dt class="varlistentry">

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

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

      <p class="para">
       The unified patch string. It has to be created using <a href="function.xdiff-string-diff.php" class="function">xdiff_string_diff()</a>,
       <a href="function.xdiff-file-diff.php" class="function">xdiff_file_diff()</a> functions or compatible tools.
      </p>
     </dd>

   
    <dt class="varlistentry">

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

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

      <p class="para">
       <i><tt class="parameter">flags</tt></i>
 can be either
       <b><tt class="constant">XDIFF_PATCH_NORMAL</tt></b> (default mode, normal patch)
       or <b><tt class="constant">XDIFF_PATCH_REVERSE</tt></b> (reversed patch).
      </p>
      <p class="para">
       Starting from version 1.5.0, you can also use binary OR to enable
       <b><tt class="constant">XDIFF_PATCH_IGNORESPACE</tt></b> flag.
      </p>
     </dd>

   
    <dt class="varlistentry">

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

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

      <p class="para">
       If provided then rejected parts are stored inside this variable.
      </p>
     </dd>

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


 <a name="function.xdiff-string-patch.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the patched string, or <b><tt class="constant">FALSE</tt></b> on error.
  </p>
 </div>


 <a name="function.xdiff-string-patch.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_patch()</b> example</b></p>
    <div class="example-contents para"><p>
     The following code applies changes to some article.
    </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">$diff&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'patch'</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;patch&nbsp;to&nbsp;html&nbsp;form&nbsp;*/<br /><br /></span><span style="color: #0000BB">$errors&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$new_article&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xdiff_string_patch</span><span style="color: #007700">(</span><span style="color: #0000BB">$old_article</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">XDIFF_PATCH_NORMAL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errors</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">$new_article</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"New&nbsp;article:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$new_article</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$errors</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Rejects:&nbsp;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$errors</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-patch.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-diff.php" class="function" rel="rdfs-seeAlso">xdiff_string_diff()</a> - Make unified diff of two strings</li>
   </ul><p>
  </p>
 </div>

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