关闭

判断两个字串有何变化

标签: functionjoin
332人阅读 评论(0) 收藏 举报

//windows 下调用 系统 命令 fc
//LINUX 下调用系统命令 diff

 

 

//此函数是针对gb码的

if(! function_exists("TextDiff")):
        function TextDiff($old, $new) {
                $mode = true;
                if(! is_array($old)) {
                        $bodyA = explode("/n", $old);
                        $bodyB = explode("/n", $new);
                        $mode = false;
                }else {
                        $bodyA = $old;
                        $bodyB = $new;
                }

                $result['added'] = array_diff_assoc($bodyA, $bodyB);
                $result['deleted'] = array_diff_assoc($bodyB, $bodyA);
                if($mode)
                        return $result;
                if(count($result['added']) >= count($result['deleted']))
                        $ar = $result['added'];
                else
                        $ar = $result['deleted'];
                $retval = '';
                foreach($ar as $k=>$v) {
                        $added = isset($result['added'][$k]) ? $result['added'][$k] : "";
                        $deleted = isset($result['deleted'][$k]) ? $result['deleted'][$k] : "";
                        preg_match_all("/[/x81-/xfe]?./",$added,$a1);

                        preg_match_all("/[/x81-/xfe]?./",$deleted,$b1);
                        $p = TextDiff($b1[0],$a1[0]);
                        foreach($p['added'] as $key=>$value){
                                $b1[0][$key] = "<font color=lime>$value</font>";
                        }
                        $deleted = join("",$b1[0]);
                        foreach($p['deleted'] as $key=>$value){
                                $a1[0][$key] = "<s><font color=red>$value</font></s>";
                        }
                        $added = join("",$a1[0]);
                        $retval .= "<br/><br/>第".($k+1)."行";
                        $retval .= "<br/>[原文]$added";
                        $retval .= "<br/>[新文]$deleted";
                }
                return $retval;
        }
endif; 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6252次
    • 积分:76
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:9篇
    • 译文:0篇
    • 评论:0条
    文章存档