function htmlDiff($old, $new)
{
$ret = '';
$diff = diff(explode(' ', $old), explode(' ', $new));
foreach($diff as $k)
{
if(is_array($k))
{
$ret .= (!empty($k['d'])?"<del style='background:#FFFF00;'>".implode(' ',$k['d'])."</del> ":''). (!empty($k['i'])?"<ins style='background:#00FF00;'>".implode(' ',$k['i'])."</ins> ":'');
}
else $ret .= $k . ' ';
}
return $ret;
}
function diff($old, $new)
{
$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)));
}
从上面的文本比较函数中如何排除一些数组值。例如:我需要通过排除数组来比较两个文本与htmlDiff(“组合和长”,“组合文本和长文本”)(“和“,”“尽快”,“。”
答案 0 :(得分:0)
您可以使用:
$arr = array("and", "have");
if(!in_array($yourVar, $arr)) {
.. compare
}
答案 1 :(得分:0)
有点难以理解矩阵比较究竟如何工作..但我想这样的事情
function htmlDiff($old, $new, $exceptions = array()) {
$ret = '';
$diff = diff(explode(' ', $old), explode(' ', $new), $exceptions);
foreach ($diff as $k) {
if (is_array($k)) {
$ret .= (!empty($k['d']) ? "<del style='background:#FFFF00;'>" . implode(' ', $k['d']) . "</del> " : '') . (!empty($k['i']) ? "<ins style='background:#00FF00;'>" . implode(' ', $k['i']) . "</ins> " : '');
}
else {
$ret .= $k . ' ';
}
}
return $ret;
}
function diff($old, $new, $exceptions = 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]) && in_array($ovalue, $exceptions) ? $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)));
}