• PHP Video Tutorials

PHP - xdiff string_patch() Function



xdiff_string_patch() function patches a string with an unified diff.

Syntax

string xdiff_string_patch( string $str , string $patch [, int $flags [, string &$error ]] )

xdiff_string_patch() function can patch a string with a unified patch in the patch parameter and return the result. The patch has to be an unified diff created by xdiff_file_diff()/xdiff_string_diff(). An optional "flags" parameter can specify the mode of operation. Any rejected parts of a patch can be stored inside an error variable if it is provided.

xdiff_string_patch() function can return a patched string or false on error.

Example

<?php
   $old_article = file_get_contents("./old_article.txt");
   $diff = $_SERVER["patch"];

   $errors = "";

   $new_article = xdiff_string_patch($old_article, $diff, XDIFF_PATCH_NORMAL, $errors);
   
   if(is_string($new_article)) {
      echo "New article:\n";
      echo $new_article;
   }

   if(strlen($errors)) {
      echo "Rejects: \n";
      echo $errors;
   }
?>
php_function_reference.htm
Advertisements