盒子
盒子
文章目录
  1. funcompare
  2. Installation
  3. Usage
    1. compareText()
    2. compareJson()
    3. css
    4. wrapper()
  • License
  • 【funcompare】PHP文本/Json差异对比工具

    funcompare

    A tool compare text differences

    Installation

    1
    composer require "funsoul/funcompare: ~1.1"

    Usage

    compareText()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    use Funsoul\Funcompare\Funcompare;

    $old = 'A tool compare text differences is funny';
    $new = 'A tool that compare text differences';

    $fc = new Funcompare();
    $res = $fc->compareText($old, $new);
    echo $res;

    // A tool <span class="new-word">that</span> compare text differences <span class="old-word">is</span> <span class="old-word">funny</span>

    compareJson()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    use Funsoul\Funcompare\Funcompare;

    $old = '[{"id":1,"name":"xxx","age":18,"cart":[{"id":100,"name":"rice"}]},{"id":2,"name":"aaa","age":18}]';
    $new = '[{"id":1,"name":"yyy","age":20,"cart":[{"id":100,"name":"banana"}]},{"id":2,"name":"bbb","age":18}]';

    $fc = new Funcompare();
    $res = $fc->compareJson($old, $new);
    echo $res

    // [{"name":{"old":"<span class=\"old-word\">xxx</span>","new":"<span class=\"new-word\">yyy</span>"},"age":{"old":"<span class=\"old-word\">18</span>","new":"<span class=\"new-word\">20</span>"},"cart":[{"name":{"old":"<span class=\"old-word\">rice</span>","new":"<span class=\"new-word\">banana</span>"}}]},{"name":{"old":"<span class=\"old-word\">aaa</span>","new":"<span class=\"new-word\">bbb</span>"}}]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    [
    {
    "name":{
    "old":"<span class="old-word">xxx</span>",
    "new":"<span class="new-word">yyy</span>"
    },
    "age":{
    "old":"<span class="old-word">18</span>",
    "new":"<span class="new-word">20</span>"
    },
    "cart":[
    {
    "name":{
    "old":"<span class="old-word">rice</span>",
    "new":"<span class="new-word">banana</span>"
    }
    }
    ]
    },
    {
    "name":{
    "old":"<span class="old-word">aaa</span>",
    "new":"<span class="new-word">bbb</span>"
    }
    }
    ]

    css

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <style>
    .new-word{background:rgba(245,255,178,1.00)}
    .new-word:after{content:' '; background:rgba(245,255,178,1.00)}
    .old-word{text-decoration:none; position:relative}
    .old-word:after{
    content: ' ';
    font-size: inherit;
    display: block;
    position: absolute;
    right: 0;
    left: 0;
    top: 55%;
    bottom: 30%;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    }
    </style>

    wrapper()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    use Funsoul\Funcompare\Funcompare;

    $old = 'A tool compare text differences is funny';
    $new = 'A tool that compare text differences';

    $fc = new Funcompare();
    $res = $fc->wrapper('[',']','<','>')->compareText($old, $new);
    echo $res;

    // A tool <that> compare text differences [is] [funny]

    License

    MIT

    请我喝一杯咖啡
    扫一扫,支持funsoul
    • 微信扫一扫