GithubHelp home page GithubHelp logo

baraja-core / simple-php-diff Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 1.0 133 KB

Find the quick difference between two text files in PHP.

Home Page: https://php.baraja.cz

License: MIT License

PHP 100.00%
diff compare php php-diff comparison comparison-modes default-theme html

simple-php-diff's Introduction

Simple PHP diff

Find the quick difference between two text files in PHP.

Idea

The library compares two text files very quickly and returns the object with the differences.

The difference has numbered lines for easy display of changes to the user. You can also read an array of changed rows as an integer array from the Diff object as you browse for changes.

๐Ÿ“ฆ Installation

It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.

To install, simply use the command:

$ composer require baraja-core/simple-php-diff

You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.

Example

Default theme

Diff can be rendered to HTML (with native method SimpleDiff::renderDiff($diff):

Default theme

How to use

Simply create a SimpleDiff instance and compare the two files:

$left = 'First text';
$right = 'Second text';

$diff = (new \Baraja\DiffGenerator\SimpleDiff)->compare($left, $right);

// simple render diff
echo '<code><pre>'
     . htmlspecialchars((string) $diff)
     . '</pre></code>';

The compare() method returns a complete object Diff with the results of the comparison, from which you can get much more.

For example, to get a list of changed rows:

echo 'Changed lines: ';
echo implode(', ', $diff->getChangedLines());

Display the Diff in HTML

Very often we need to display the differences directly in the browser, for this the native method renderDiff() is suitable.

$left = 'First text';
$right = 'Second text';

$simpleDiff = new \Baraja\DiffGenerator\SimpleDiff;
$diff = $simpleDiff->compare($left, $right);

echo $simpleDiff->renderDiff($diff);

The method accepts Diff and returns valid treated HTML that can be displayed directly to the user.

Comparison mode

This tool supports strict and basic comparison modes (strict mode is disabled by default). Strict mode also allows you to compare changes in different line wrapping methods (for example, "\n" and so on).

๐Ÿ“„ License

baraja-core/simple-php-diff is licensed under the MIT license. See the LICENSE file for more details.

simple-php-diff's People

Contributors

imgbot[bot] avatar janbarasek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alliline

simple-php-diff's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.