Php 通过排除数组值进行文本比较 函数htmlDiff($old,$new) { $ret=''; $diff=diff(explode(“”,$old),explode(“”,$new)); foreach(差异为$k) { if(is_数组($k)) { $ret.=(!empty($k['d'])?“”。内爆(“”,$k['d']):“”)。(!empty($k['i'])?“”。内爆(“”,$k['i']):“”); } 否则$ret.=$k.'; } 返回$ret; } 功能差异($旧,$新) { $maxlen=0; foreach($oindex=>$ovalue) { $nkeys=数组_键($new,$ovalue); foreach($nkeys作为$nindex) { $matrix[$oindex][$nindex]=isset($matrix[$oindex-1][$nindex-1])?$matrix[$oindex-1][$nindex-1]+1:1; 如果($matrix[$oindex][$nindex]>$maxlen) { $maxlen=$matrix[$oindex][$nindex]; $omax=$oindex+1-$maxlen; $nmax=$nindex+1-$maxlen; } } } if($maxlen==0)返回数组(数组('d'=>$old,'i'=>$new)); 返回数组合并( diff(数组切分($old,0,$omax),数组切分($new,0,$nmax)), 数组\u片($new、$nmax、$maxlen), diff(阵列片($old,$omax+$maxlen),阵列片($new,$nmax+$maxlen)); }

Php 通过排除数组值进行文本比较 函数htmlDiff($old,$new) { $ret=''; $diff=diff(explode(“”,$old),explode(“”,$new)); foreach(差异为$k) { if(is_数组($k)) { $ret.=(!empty($k['d'])?“”。内爆(“”,$k['d']):“”)。(!empty($k['i'])?“”。内爆(“”,$k['i']):“”); } 否则$ret.=$k.'; } 返回$ret; } 功能差异($旧,$新) { $maxlen=0; foreach($oindex=>$ovalue) { $nkeys=数组_键($new,$ovalue); foreach($nkeys作为$nindex) { $matrix[$oindex][$nindex]=isset($matrix[$oindex-1][$nindex-1])?$matrix[$oindex-1][$nindex-1]+1:1; 如果($matrix[$oindex][$nindex]>$maxlen) { $maxlen=$matrix[$oindex][$nindex]; $omax=$oindex+1-$maxlen; $nmax=$nindex+1-$maxlen; } } } if($maxlen==0)返回数组(数组('d'=>$old,'i'=>$new)); 返回数组合并( diff(数组切分($old,0,$omax),数组切分($new,0,$nmax)), 数组\u片($new、$nmax、$maxlen), diff(阵列片($old,$omax+$maxlen),阵列片($new,$nmax+$maxlen)); },php,arrays,comparison,Php,Arrays,Comparison,从上面的文本比较函数中,如何排除一些数组值。例如:我需要通过排除数组(“and”,“have”,“ASEAN”)来将两个文本与htmlDiff(“组合和长”,“组合文本和长文本”)进行比较。您可以使用: function htmlDiff($old, $new) { $ret = ''; $diff = diff(explode(' ', $old), explode(' ', $new)); foreach($diff as $k) { if(is_array($k)) {

从上面的文本比较函数中,如何排除一些数组值。例如:我需要通过排除数组(“and”,“have”,“ASEAN”)来将两个文本与htmlDiff(“组合和长”,“组合文本和长文本”)进行比较。

您可以使用:

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)));
    }

有点难以理解矩阵比较到底是如何工作的。。但我想是这样的


$arr = array("and", "have");
if(!in_array($yourVar, $arr)) {
  .. compare
}
函数htmlDiff($old,$new,$exceptions=array()){ $ret=''; $diff=diff(explode(“”,$old),explode(“”,$new),$exceptions); foreach(差异为$k){ if(is_数组($k)){ $ret.=(!empty($k['d'])?“”。内爆(“”,$k['d']):“”)。(!empty($k['i'])?“”。内爆(“”,$k['i']):“”); } 否则{ $ret.=$k.'; } } 返回$ret; } 函数diff($old,$new,$exceptions=array()){ $maxlen=0; foreach($oindex=>$ovalue){ $nkeys=数组_键($new,$ovalue); foreach($nkeys作为$nindex){ $matrix[$oindex][$nindex]=isset($matrix[$oindex-1][$nindex-1])&in_数组($ovalue,$exceptions)?$matrix[$oindex-1][$nindex-1]+1:1; 如果($matrix[$oindex][$nindex]>$maxlen){ $maxlen=$matrix[$oindex][$nindex]; $omax=$oindex+1-$maxlen; $nmax=$nindex+1-$maxlen; } } } 如果($maxlen==0){ 返回数组( 排列( 'd'=>$old, “i”=>$new ) ); } 返回数组合并( diff(数组切分($old,0,$omax),数组切分($new,0,$nmax)), 数组\u片($new、$nmax、$maxlen), diff(阵列片($old,$omax+$maxlen),阵列片($new,$nmax+$maxlen)); }
可能希望将此标记为PHP问题您尝试使用in_array()方法进行此操作?我可以将上述代码放在哪里。请用完整的代码进行澄清。结果应该是全文,不突出显示数组值(ins或del)。我的想法是找出ins标记和del标记,并将数组值替换为“some value”或“some value”. 如何使用数组获取所有标记并替换标记中的特定单词。?
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)));
}