php怎么比较两个字符串

如题所述

$a="abcde12";
$b="abcdf6";
echo "<pre>";
var_dump(diffStr($a,$b));
echo "</pre>";
/*
*比较字符串不同的字符
*@参数:$str1:第一个字符串,$str2:第二个字符串
*@<a href="https://www.baidu.com/s?wd=%E8%BF%94%E5%9B%9E%E5%80%BC&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YzmvR3mhuWmHfzuyR3nvnk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErH61PW6snWfkP1fsPjfznHT4" target="_blank" class="baidu-highlight">返回值</a>:不同字符串的数组,
*/
function diffStr($str1,$str2){
$sArr1 = str_split($str1);
$sArr2 = str_split($str2);

$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;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-03
$a='abc';
$b='def';

if ($a==$b) ...
if ($a===$b) ...

if (strcmp($a, $b)==0) ...
if (strncmp($a, $b, 3)==0) ...

等等,好多。你在百度上搜索php 比较两个字符串,查找这方面的文章。本回答被提问者和网友采纳
相似回答