Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comparez deux tableaux multidimensionnels différents et mettez en évidence les changements

D'après ce que j'ai compris, vous cherchez un moyen de mettre en évidence les différences dans le texte pour voir les différences entre les descriptions. À partir de là, puis-je suggérer que vous limitiez la fonction diff à b_discription car les autres attributs ne semblent pas être dans le cadre de ce dont vous avez besoin. Attendez-vous business_logo , business_name ou mod_date être changé? Quoi qu'il en soit, la fonction suivante peut être exécutée sur tous les attributs de votre objet s'ils peuvent être convertis en chaîne.

simplediff est un excellent script qui trouve la différence entre deux chaînes, notamment :

  • Existe en A et non en B (suppression)
  • Existe en B et non en A (Insertion)
<?php
/*
    Paul's Simple Diff Algorithm v 0.1
    (C) Paul Butler 2007 <http://www.paulbutler.org/>
    May be used and distributed under the zlib/libpng license.
*/
function diff($old, $new){
    $matrix = array();
    $maxlen = 0;
    foreach($old as $oindex => $ovalue){
        $nkeys = array_keys($new, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
    return array_merge(
        diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function htmlDiff($old, $new){
    $ret = '';
    $diff = diff(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}

$old = "We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly";

$new = "We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly, which enables you to send simultaneous bulk sms to your targeted Customers, Regular-Customers, Buyers, Shoppers, Fans, Regular-shoppers, Clients, Clientele, Members, Managers, Supervisors, Fieldworkers, Graduates, Post graduates, Technicians, Public, Citizens, Mobile-Users, Viewers, Future-purchasers, Users, End-users, Students, Job-seekers, Enjoyers, Visitors, Frequent-visitors, Persons, Individuals, Frequenter, Obtainers, Receivers, Assignees, Recipients, Travelers, Tourists, Guys, Persons, Men and Women, Spectators, Technicians, Staff, Workers, Recruiters, Newcomers, Representatives, Dealers, Distributors, Followers, Shareholders, Investors, Bondholders, Shareowners, Financiers, Bankers, Participants, Associates, Assistants, Colleagues, Contributors, Helpers, Partakers, Party, Sharers, Supporters, Admirers, Devotees, Groupies, Enthusiasts and many more.";

?>
<!doctype html>
<head>
    <style>
        .container {
            width: 50%;
            margin-right: auto;
            margin-left: auto;
            font-family: sans-serif;
            font-size: 12px;
            line-height: 16px;
        }

        del {
            background-color: #FFAB91;
            color: ;
            text-decoration: none;
        }

        ins {
            background-color: #C5E1A5;
            color: ;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <?php echo htmlDiff($old, $new); ?>
    </div>  
</body>
</head>

Exemple de travail :https://3v4l.org/uU0dv