灬紫羽
error_reporting(E_ALL & ~E_NOTICE);//关闭notice报错
$a="你好,我是javascript";
$b="你好,我是php";
$diff = diffStr($a,$b);
echo "<pre>";
print_r($diff);
/* 打印结果如下:
Array
(
[0] => Array
(
[s1] => j
[s2] => p
)
[1] => Array
(
[s1] => a
[s2] => h
)
[2] => Array
(
[s1] => v
[s2] => p
)
[3] => Array
(
[s1] => a
)
[4] => Array
(
[s1] => s
)
[5] => Array
(
[s1] => c
)
[6] => Array
(
[s1] => r
)
[7] => Array
(
[s1] => i
)
[8] => Array
(
[s1] => p
)
[9] => Array
(
[s1] => t
)
)
*/
/*
*比较字符串不同的字符
*@参数:$str1:第一个字符串,$str2:第二个字符串
*@返回值:不同字符串的数组,
*/
function diffStr($str1,$str2){
preg_match_all("/./u", $str1, $arr1);
preg_match_all("/./u", $str2, $arr2);
$sArr1 = $arr1[0];
$sArr2 = $arr2[0];
$num1 = count($sArr1);
$num2 = count($sArr2);
$aNew = array();
if($num1 > $num2){
foreach($sArr1 as $k=>$val){
if($num2 > $k && $val != $sArr2[$k]){
$aNew[] = array('s1'=>$val,'s2'=>$sArr2[$k]);
}elseif($num2 <= $k){
$aNew[] = array("s1"=>$val);
}
}
}elseif($num1 < $num2){
foreach($sArr2 as $k=>$val){
if($num1 > $k && $val != $sArr1[$k]){
$aNew[] = array('s1'=>$sArr1[$k],'s2'=>$val);
}elseif($num1 <= $k){
$aNew[] = array("s2"=>$val);
}
}
}elseif($num1 == $num2){
foreach($sArr1 as $k=>$val){
if($val != $sArr2[$k]){
$aNew[] = array('s1'=>$val,'s2'=>$sArr2[$k]);
}
}
}
return $aNew;
}