File comparison extension in PHP

Time:2021-10-23

The file comparison extension is rarely used now, because in most cases we use some code management tools, such as git or SVN. In fact, its function is very similar to this kind of tools. In addition, there is a very common beyond compare tool, which can also facilitate file comparison.

Installation and preparation

This file extension in PHP is called xdiff extension, which can be downloaded and installed directly in PECL.

It should be noted that the libxdiff tool needs to be installed in the operating system to install this extension. The official website address of this tool is available in the reference link at the bottom of the article. Libxdiff cannot be installed using the default yum, so you need to download it and install it yourself. Like other Linux tools, the installation process is very simple and will not be repeated here.

Xdiff extension supports the difference comparison between string and file and some related operations. Here we will mainly explain the operation of string. The operation related to file will give the usage of all operation functions at the end. First, we need to define some strings and related files for subsequent operations.

$old_ Article = "I'm not a wild guest. I wander around the world.
He was ordered to live in a famous mountain for a while. It's quite flexible.
Seeing through the disturbance of dust and labor, how can you enjoy leisure.
Liu Xia drank carefully and chanted poems. And accompanied by white clouds. ";
$new_ Article = "I'm not a wild guest. I wander around the world.
He was ordered to live in a famous mountain for a while. It's quite flexible.
Seeing through the disturbance of dust and labor, how can you enjoy leisure.一
Liu Xia drank carefully and chanted poems. And accompanied by white clouds. ";
$new_ Article1 = "I'm not a wild guest. I wander around the world.
He was ordered to live in a famous mountain for a while. It's quite flexible.二
Seeing through the disturbance of dust and labor, how can you enjoy leisure.
Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
Three one four one ";

file_put_contents('old_file.txt', $old_article);
file_put_contents('new_file.txt', $new_article);
file_put_contents('new_file1.txt', $new_article1);

String difference

$diff = xdiff_string_diff($old_article, $new_article);
var_dump($diff);
// string(273) "@@ -1,4 +1,4 @@
//I'm not a wild guest. I'm floating around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//- see through the disturbance of dust and labor, how can you enjoy leisure.
//+ see through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
// \ No newline at end of file
// "

Use xdiff_ string_ The diff () function can get the difference information in the two strings. You can see that its content structure is very similar to that of GIT. For example, the differences in the line represented by +, – signs are not unfamiliar as long as we have used git or SVN.

Merge string

var_dump(xdiff_string_merge3($old_article, $new_article, $new_article1, $error));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "
var_dump($error); // NULL

xdiff_ string_ The merge3 () function is used to merge three strings together, which is also similar to the merge function in GIT. However, this function requires three strings, but through the test, we found that there is only the first \ $new_ Article and original $old_ The article merge succeeded. Third $NEW_ Article1 is not merged into the last returned string. The function of this function is inconsistent with the actual effect. No relevant reference materials have been found. The introduction of the official documents is also very simple. Therefore, if you know the real and specific situation of this function, you can leave a message to discuss it together!

The $error parameter is an optional reference parameter. It will return an error message if there is any problem during the merge process.

Patch data (patch)

var_dump(xdiff_string_patch($old_article, $diff, XDIFF_PATCH_NORMAL, $errors));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "
var_dump($errors); // NULL

As can be seen from the patch in the name of the function, this xdiff_ string_ Patch () is used to patch difference strings. In the past desktop era, whether operating systems or various games, various patches were often updated. The patch here is actually similar to the merge difference. Its first parameter is the original string, and its second parameter is xdiff_ string_ The result of patching the difference data generated by diff() is to return the string after the formal full union difference.

The third parameter is optional and can also be defined as xdiff_ PATCH_ Reverse, that is, reverse the patch, returns only the original data and does not return the results after difference consolidation. Conversely, using this parameter, we can set the first parameter to the modified $new_ Article, and then reverse the original data. You can try it yourself. The last parameter is also an error variable of optional reference type.

Binary patch data

$patchBinary = xdiff_string_bdiff($old_article, $new_article);
var_dump($patchBinary);
//String (44) "{n}
//Let's drink! "

var_dump(xdiff_string_bdiff_size($patchBinary)); // int(180)
var_dump(xdiff_string_bpatch($old_article, $patchBinary));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "

In addition to the original string operation, we can also use xdiff_ string_ Bdiff() returns binary string difference results. Similarly, use xdiff_ string_ Bpatch () can patch the binary string operation results, that is, merge differences. In addition, there is a function xdiff in the binary operation_ string_ bdiff_ Size() is used to return the character length in the result returned by the binary difference function.

$raPatchBinary = xdiff_string_rabdiff($old_article, $new_article1);
var_dump($raPatchBinary);
//String (46) "{n × 2XY"
//Three one four one“

var_dump(xdiff_string_bdiff_size($raPatchBinary)); // int(193)
var_dump(xdiff_string_bpatch($old_article, $raPatchBinary));
//String (193) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible. two
//Seeing through the disturbance of dust and labor, how can you enjoy leisure.
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
//Three one four one“

Finally, there is an xdiff_ string_ Rabdiff() also returns binary data difference information. It and xdiff_ string_ The main difference between bdiff () is the different algorithms used.

File operation

Above, we introduced the operation of xdiff extension on strings in detail. It also provides a series of file operations. Using these functions to directly operate files is really very similar to our GIT and other tools.

$old_file = 'old_file.txt';
$new_file = 'new_file.txt';
$new_file1 = 'new_file1.txt';
$diff_file = 'file.diff';
$merge_file = 'merge.txt';
$patch_file = 'patch.diff';

echo "File Diff: ", PHP_EOL;
$patch = xdiff_file_diff($old_file, $new_file, $diff_file);
var_dump($patch); // bool(true)
var_dump(file_get_contents($diff_file));
// string(273) "@@ -1,4 +1,4 @@
//I'm not a wild guest. I'm floating around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//- see through the disturbance of dust and labor, how can you enjoy leisure.
//+ see through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
// \ No newline at end of file
// "

echo 'File Merge: ', PHP_EOL;
var_dump(xdiff_file_merge3($old_file, $new_file,  $new_file1, $merge_file));
// string(307) "@@ -1,4 +1,5 @@
//I'm not a wild guest. I'm floating around the world.
//- he was ordered to live in a famous mountain for a while. It's quite flexible.
//+ he was ordered to live in a famous mountain for a while. It's quite flexible. two
//Seeing through the disturbance of dust and labor, how can you enjoy leisure.
//- a poem written by Liu Xia. And accompanied by white clouds+ Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
//+ three one four one“
var_dump(file_get_contents($merge_file));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "

echo "File Patch: ", PHP_EOL;
var_dump(xdiff_file_patch($old_file, $diff_file, $patch_file, XDIFF_PATCH_NORMAL)); // bool(true)
var_dump(file_get_contents($patch_file));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "

echo "File Binary Diff: ", PHP_EOL;
$patchBinary = xdiff_file_bdiff($old_file, $new_file, $diff_file);
var_dump($patchBinary); // bool(true)
var_dump(file_get_contents($diff_file));
//String (44) "{n}
//Let's drink! "

var_dump(xdiff_file_bdiff_size($diff_file)); // int(180)
var_dump(xdiff_file_bpatch($old_file,$patchBinary, $patch_file)); // bool(false)
var_dump(file_get_contents($patch_file));
//String (180) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.
//Seeing through the disturbance of dust and labor, how can you enjoy leisure. one
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds. "

echo "File RA Binary Diff: ", PHP_EOL;
$raPatchBinary = xdiff_file_rabdiff($old_file, $new_file1, $diff_file);
var_dump($raPatchBinary); // bool(true)
var_dump(file_get_contents($diff_file));
//String (46) "{n × 2XY"
//Three one four one“

var_dump(xdiff_file_bdiff_size($diff_file)); // int(193)
var_dump(xdiff_file_bpatch($old_file, $raPatchBinary, $patch_file)); // bool(false)
var_dump(file_get_contents($patch_file));
//String (193) "I'm not a wild guest. I wander around the world.
//He was ordered to live in a famous mountain for a while. It's quite flexible.二
//Seeing through the disturbance of dust and labor, how can you enjoy leisure.
//Liu Xia drank carefully and chanted poems. And accompanied by white clouds.
//Three one four one“

Here we will not explain them one by one. The operations and functions of these functions are similar to those of string operations, but the parameters are slightly different. For example, after comparing, merging and patching, they will generate a file, and the parameters of all functions are file-based. You can run the test code by yourself and refer to the official documents for learning.

summary

In fact, we don’t use this xdiff extension much, but we have seen a set of open source extensions used in the function of managing front-end template pages in CMS system using PHP. The existence of any tool has its meaning. Maybe you just saw this article when you are distressed about a function, so as to easily solve the problem at hand. It is uncertain to know and have a general impression, so as not to be blind in work. This is the meaning of our document brushing.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/10.PHP File comparison extension.php in

Reference documents:

https://www.php.net/manual/zh/book.xdiff.php

https://directory.fsf.org/wiki/LibXDiff

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532