完整代码如下。
<?php
function dif($row, $zz){
if(sizeof($zz)==0){
return 1;
}
//print_r($row);
//echo 'PK<br/>';
foreach($zz as $row2){
if( ($row2[0]+$row2[2] <= $row[0] && $row2[1]+$row2[2] <= $row[1]) || ($row[0]+$row[2] <= $row2[0] && $row[1]+$row[2] <= $row2[1]) ){
//echo "wwwwwwwww";
}else{
return 0;
}
}
return 1;
}
function str_insert($str, $i, $substr)
{
for($j=0; $j<$i; $j++){
$startstr .= $str[$j];
}
for ($j=$i; $j<strlen($str); $j++){
$laststr .= $str[$j];
}
$str = ($startstr . $substr . $laststr);
return $str;
}
$str1 = "丁丁是个很好的人";
$str2 = "小明是不错的孩子";
echo '<pre>';
$k = 0;
for( $j = 0; $j < strlen($str1); $j++ ){
for( $i = 0; $i < strlen($str2); $i++ ){
if( $str1[$j] == $str2[$i] ){
$node[$k] = array($j,$i);
$k++;
}
}
}
//echo '#'.sizeof($node).'#<br/>';
for( $m = 0; $m < sizeof($node); $m++){
$n = 0;
do{
$n++;
}while( (($node[$m][0]+$n) < strlen($str1)) && ($str1[$node[$m][0]+$n] == $str2[$node[$m][1]+$n]) );
$arr2[$m] = $n;
$node[$m][2] = $n;
}
array_multisort($arr2, SORT_DESC, $node);
//print_r($node);
$x = 0;
foreach($node as $row){
if( dif($row, $zz)){
$zz[$x] = $row;
//print_r($row);
$x++;
}
}
foreach($zz as $key => $t1231){
$arr3[$key] = $t1231[0];
};
//print_r($zz);
array_multisort($arr3, SORT_DESC, $zz);
//echo '##########<br/>';
//print_r($arr3);
//print_r($zz);
echo '</pre>';
foreach($zz as $value){
$str1 = str_insert($str1, $value[0]+$value[2], "</b>");
$str1 = str_insert($str1, $value[0], "<b style='color:red'>");
$str2 = str_insert($str2, $value[1]+$value[2], "</b>");
$str2 = str_insert($str2, $value[1], "<b style='color:red'>");
}
echo 'A: '.$str1.'<br/>';
echo 'B: '.$str2.'<br/>';
?>