Php 如果通过比较字符串发现删除了中间的子字符串,如何突出显示下一个子字符串和上一个子字符串?

Php 如果通过比较字符串发现删除了中间的子字符串,如何突出显示下一个子字符串和上一个子字符串?,php,string,comparison,Php,String,Comparison,我正在比较PHP中的两个字符串。在修改后的新字符串中,我正在编写代码,用于通过与旧字符串的比较突出显示更改。但是我被困在这里了 例如: 旧字符串:这是我的内容 新字符串:这是我的内容 在这里,我想要这样的东西 新字符串:这里是我的内容 我已尝试通过保存以前的单词来匹配单词。但什么都不管用 class ContentController extends Controller { public function diffArray($old, $new){ $matrix = array

我正在比较PHP中的两个字符串。在修改后的新字符串中,我正在编写代码,用于通过与旧字符串的比较突出显示更改。但是我被困在这里了

例如:

旧字符串:这是我的内容

新字符串:这是我的内容

在这里,我想要这样的东西

新字符串:这里是我的内容

我已尝试通过保存以前的单词来匹配单词。但什么都不管用

class ContentController extends Controller
{ 
 public function diffArray($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(
        self::diffArray(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        self::diffArray(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}

public function htmlDiff($old, $new){
    $ret = '';
    $diff = self::diffArray(explode(' ', $old), explode(' ', $new));
    foreach($diff as $k){
        if(is_array($k)){
            $ret .= /*(!empty($k['d'])?'<del style="background-color:#ffcccc">'.implode(' ',$k['d']).'</del> ':'').*/(!empty($k['i'])?'<span style="background-color:#ccffcc">'.implode(' ',$k['i']).'</span> ':'');
        }else{
            $ret .= $k . ' ';
        }
    }
    return $ret;
}

$old_content = "Here is the my content";
$new_content = "Here is my content";
$highlighted_content = ContentController::htmlDiff($old_content, $new_content);

}
类ContentController扩展控制器
{ 
公共函数diffArray($old,$new){
$matrix=array();
$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));
返回数组合并(
self::diffArray(数组切分($old,0,$omax),数组切分($new,0,$nmax)),
数组\u片($new、$nmax、$maxlen),
self::diffArray(array_slice($old,$omax+$maxlen),array_slice($new,$nmax+$maxlen));
}
公共函数htmlDiff($old,$new){
$ret='';
$diff=self::diffArray(explode(“”,$old),explode(“”,$new));
foreach(差异为$k){
if(is_数组($k)){
$ret.=/*(!empty($k['d'])?“”。内爆(“”,$k['d']):“”)。*/(!empty($k['i'])?“”。内爆(“”,$k['i']):“”);
}否则{
$ret.=$k.';
}
}
返回$ret;
}
$old_content=“这是我的内容”;
$new\u content=“这是我的内容”;
$highlighted_content=ContentController::htmlDiff($old_content,$new_content);
}
试试这个:

Nb:我没有阻止我的代码注意到错误,请确保已定义密钥

$old=“这是我的内容”;
$new=“这是我的内容”;
$old_exp=爆炸(“”,$old);
$new_exp=爆炸(“”,$new);
$del_key=array_key((array_diff($old_exp,$new_exp));
变量转储($del_key);
foreach($del_key作为$old_key){
$new_-exp[$old_-key-1]=”。$new_-exp[$old_-key-1]。”;
$new_-exp[$old_-key]=”.$new_-exp[$old_-key]。”;
}
$new\u str=内爆(“,$new\u exp”);
echo$new_str;
希望对你有帮助


玩得开心:)

解决了。经过数小时的编码,我得到了这个。这是修改后的代码

public function diffArray($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(
        self::diffArray(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        self::diffArray(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}

function htmlDiff($old, $new){
    $diff = self::diffArray(explode(' ', $old), explode(' ', $new));
    $flag = 0;
    $ret = "";
    foreach($diff as $i => $k){
        if(is_array($k)){
            if((count($k['d']) != 0) && (count($k['i']) == 0)){
                $flag = 1;
            }
            else if((count($k['d']) != 0) && (count($k['i']) != 0)){
                $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
            }
            else if((count($k['d']) == 0) && (count($k['i']) != 0)){
                $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
            }
        }
        else{
            if($flag == 0){
                $ret .= $k . ' ';
            }
            else if($flag == 1){
                $pre = $i - 2;
                $post = $i;
                $ret= preg_replace('/\W\w+\s*(\W*)$/', '$1', $ret);
                $ret .= " <span style='background-color:#ccffcc'>".$diff[$pre]." ".$diff[$post]."</span> ";
            }
            $flag = 0;
        }
    }
    return $ret;
}

$old_content = "Here is the my content";
$new_content = "Here is my content";
$highlighted_content = ContentController::htmlDiff($old_content, $new_content);
公共函数diffArray($old,$new){
$matrix=array();
$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));
返回数组合并(
self::diffArray(数组切分($old,0,$omax),数组切分($new,0,$nmax)),
数组\u片($new、$nmax、$maxlen),
self::diffArray(array_slice($old,$omax+$maxlen),array_slice($new,$nmax+$maxlen));
}
函数htmlDiff($old,$new){
$diff=self::diffArray(explode(“”,$old),explode(“”,$new));
$flag=0;
$ret=“”;
foreach($i=>k){
if(is_数组($k)){
如果((计数($k['d'])!=0)和(&(计数($k['i'])==0)){
$flag=1;
}
else如果((计数($k['d'])!=0)和(&(计数($k['i'])!=0)){
$ret.=(!empty($k['i'])?“”。内爆(“”,$k['i']):“”);
}
else如果((计数($k['d'])=0)和(&(计数($k['i'])!=0)){
$ret.=(!empty($k['i'])?“”。内爆(“”,$k['i']):“”);
}
}
否则{
如果($flag==0){
$ret.=$k.';
}
else if($flag==1){
$pre=$i-2;
$post=$i;
$ret=preg_replace('/\W\W+\s*(\W*)$/'、'$1'、$ret);
$ret.=“..$diff[$pre]”.“..$diff[$post]”;
}
$flag=0;
}
}
返回$ret;
}
$old_content=“这是我的内容”;
$new\u content=“这是我的内容”;
$highlighted_content=ContentController::htmlDiff($old_content,$new_content);

Hey@Lucas。。谢谢你的回复。您的代码运行良好。但如果将字符串修改为这些,$old=“这是我的内容。我必须将其发送给我的官员。请以更好的方式更正此内容。”$new=“这是我的内容。我必须将其发送给警官。请以更好的方式更正此内容。”;它工作不正常。如果您发现有什么有用的东西,请告诉我。@HarrySingla我有一些东西,但它只适用于上一个单词(并且仅适用于新字符串中缺少的单词)@Locas,但我需要突出显示所有更改。
public function diffArray($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(
        self::diffArray(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        self::diffArray(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}

function htmlDiff($old, $new){
    $diff = self::diffArray(explode(' ', $old), explode(' ', $new));
    $flag = 0;
    $ret = "";
    foreach($diff as $i => $k){
        if(is_array($k)){
            if((count($k['d']) != 0) && (count($k['i']) == 0)){
                $flag = 1;
            }
            else if((count($k['d']) != 0) && (count($k['i']) != 0)){
                $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
            }
            else if((count($k['d']) == 0) && (count($k['i']) != 0)){
                $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
            }
        }
        else{
            if($flag == 0){
                $ret .= $k . ' ';
            }
            else if($flag == 1){
                $pre = $i - 2;
                $post = $i;
                $ret= preg_replace('/\W\w+\s*(\W*)$/', '$1', $ret);
                $ret .= " <span style='background-color:#ccffcc'>".$diff[$pre]." ".$diff[$post]."</span> ";
            }
            $flag = 0;
        }
    }
    return $ret;
}

$old_content = "Here is the my content";
$new_content = "Here is my content";
$highlighted_content = ContentController::htmlDiff($old_content, $new_content);