其实之前已经回答你了,稍微改一下就可以了。例子:
<?php
$a="我今年十三岁我很开心";
$b="我今年十二岁我开心";
echo "<pre>";
var_dump(diffStr($a,$b));
echo "</pre>";
/*
*比较字符串不同的字符
*@参数:$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;
}
?>