PHP - Fonction xdiff string_patch ()

La fonction xdiff_string_patch () corrige une chaîne avec un diff unifié.

Syntaxe

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

La fonction xdiff_string_patch () peut patcher une chaîne avec un patch unifié dans le paramètre patch et renvoyer le résultat. Le patch doit être un diff unifié créé par xdiff_file_diff () / xdiff_string_diff (). Un paramètre optionnel "flags" peut spécifier le mode de fonctionnement. Toutes les parties rejetées d'un correctif peuvent être stockées dans une variable d'erreur si elle est fournie.

La fonction xdiff_string_patch () peut renvoyer une chaîne corrigée ou false en cas d'erreur.

Exemple

<?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;
   }
?>